Given the following data:
var json = [
{ 'myKey': 'A', 'status': 0 },
{ 'myKey': 'B', 'status': 1 },
{ 'myKey': 'C', 'status': 1 },
{ 'myKey': 'D', 'status': 1 }
];
I want to append a new array with a variable “Id”, something like :
var Id = "aNewLetterFunction";
json.push({'myKey':'+Id+','status':1}); //this doesn't work
How can I do, since the +Id+ is not natively considered as a variable ?
JSfiddle.net appreciate.
EDIT: fiddle available
I tried various things such :
json.push('{"myKey":"'+Id+'","status":1},');
or
var ar1 = '{"myKey":"';
var Id = Id;
var ar2 = '","status":1},';
json.push(ar1+Id+ar2);
Create an object, don’t create a JSON string:
You don’t want to add another value to the JSON but to the array that can be parsed to a JSON string, but isn’t a string.
Live DEMO