1

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"
}
asked Jul 15, 2013 at 8:53

1 Answer 1

4

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/

answered Jul 15, 2013 at 9:00

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.