Reputation: 10466
I have an array that looks like this
[{
"Inventory": {
"dashboard_id": "Inventory",
"filter_by": "Location",
"yAxis": "Quantity",
"title": "",
"chart_type": "-------Select-------",
"mainchart": "Yes"
},
"Quality": {
"dashboard_id": "Quality",
"filter_by": "Location",
"yAxis": "SampleNo",
"title": "",
"chart_type": "-------Select-------",
"mainchart": "Yes"
}
}]
I need to add more values into each object. How do I ADD to an existing array so it might look something like this
{
"Inventory": [{
"dashboard_id": "Inventory",
"filter_by": "Location",
"yAxis": "Quantity",
"title": "",
"chart_type": "-------Select-------",
"mainchart": "Yes"
}, {
"dashboard_id": "Inventory",
"filter_by": "Location",
"yAxis": "Quantity",
"title": "",
"chart_type": "-------Select-------",
"mainchart": "Yes"
}],
"Quality": {
"dashboard_id": "Quality",
"filter_by": "Location",
"yAxis": "SampleNo",
"title": "",
"chart_type": "-------Select-------",
"mainchart": "Yes"
}
}
I add into the array using nestedData[dashId] = data;
Where dashId consist of Quality
, 'Inventory', etc.
data is
{
"dashboard_id": "Inventory",
"filter_by": "Location",
"yAxis": "Quantity",
"title": "",
"chart_type": "-------Select-------",
"mainchart": "Yes"
}
Upvotes: 1
Views: 82
Reputation: 94429
Build a new Object
using the literal syntax, which contains the desired arrays and assigns the objects from the source array to the respective property in the object.
var arr = [{
"Inventory": {
"dashboard_id": "Inventory",
"filter_by": "Location",
"yAxis": "Quantity",
"title": "",
"chart_type": "-------Select-------",
"mainchart": "Yes"
},
"Quality": {
"dashboard_id": "Quality",
"filter_by": "Location",
"yAxis": "SampleNo",
"title": "",
"chart_type": "-------Select-------",
"mainchart": "Yes"
}
}];
var obj = {Inventory:[arr[0].Inventory], Quality:[arr[0].Quality]};
obj.Inventory.push({
"dashboard_id": "Inventory",
"filter_by": "Location",
"yAxis": "Quantity",
"title": "",
"chart_type": "-------Select-------",
"mainchart": "Yes"
});
console.log(obj);
Working Example: http://jsfiddle.net/vJjSX/
Upvotes: 4