Reputation: 1430
Can't find help on this anywhere
I have an array -
var myArray = [1,2,3]
But I need this to become -
[{"val" : 1, "checked" : false}, {"val" : 2, "checked" : false},{"val" : 3, "checked" : false}]
How is this done?
Upvotes: 0
Views: 91
Reputation: 722
Try this:
var myArray = [1, 2, 3];
var jsonText = [];
for (i = 0; i < myArray.length; i++) {
jsonText[i] = {};
jsonText[i].val = myArray[i];
jsonText[i].checked = false;
}
JSON.stringify(jsonText);
Upvotes: 2
Reputation: 68433
{'val' = 1, 'checked' = false}
is not a valid JSON format since key and value should be separated by a colon :
not =
, it should be {"val" : 1, "checked" : false}
Try with the below,
var newArray = myArray.map( function(value){
return {"val" : value, "checked" : false};
} )
Upvotes: 7