I have to create an empty array with the following keys: "id", "action". How do I create it?
3 Answers
Assuming to be placed in a variable, because you have 'json' tagged. The following is some explanation of each step of a simple example breakdown:
// create an empty array
var array = [];
// create an object with properties "id", "action" with mock values
var object = {
id: "1",
action: "shout"
}
// add object to array
array.push(object);
// create encode json string
var myJSONString = JSON.stringify(array);
Hope this helps.
Do you mean this?
JSON.stringify({
id: null,
action: null
})
-
1
-
To get JSON-data? This would be the output: "{"id":null,"action":null}"– RafaelKrMay 4, 2015 at 13:25
-
Here is pure JSON example with sample data that should give you guidance
{
"object": [
{
"Id": "1",
"action": "New"
},
{
"Id": "2",
"action": "Open"
},
{
"Id": "3",
"action": "Close"
}
]
}
-
1
-
var a = {id: null, action: null};
Also, you cannot create an array with keys. It's an object, which you then can place into an array.