jquery - Add a value to an JSON array object -


i have array looks 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 add more values each object. how add existing array might 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 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" } 

build new object using literal syntax, contains desired arrays , assigns objects source array respective property in 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/


Comments