开发者

javascript manipulate json object

开发者 https://www.devze.com 2022-12-14 10:14 出处:网络
For example, I have the following JSON object json_obj1 json_obj1 = {x:{id:1,bars:{show:true,barWidth:0.4}}}

For example, I have the following JSON object json_obj1

json_obj1 = {x:{id:1,bars:{show:true,barWidth:0.4}}}

Now, how could I add the following object (using javascript):

   y:{id:2,bars:{show:true,barWidth:0.4}}

to json_obj1 so that it will be:

{x:{id:1,bars:{show:true,barWidth:0.4}开发者_如何学运维},y:{id:2,bars:{show:true,barWidth:0.4}}}


You can just set the field y of your json_obj1

json_obj1 = {x:{id:1,bars:{show:true,barWidth:0.4}}}
json_obj1.y = {id:2,bars:{show:true,barWidth:0.4}}

Now json_obj1 = {x:{id:1,bars:{show:true,barWidth:0.4}},y:{id:2,bars:{show:true,barWidth:0.4}}}


It doesn't appear as if your question actually involves JSON. The first code fragment is just a JavaScript object literal. Given your description of the problem, something like this should work:

json_obj1 = {x:{id:1,bars:{show:true,barWidth:0.4}}};
json_obj1.y = {id:2,bars:{show:true,barWidth:0.4}};

This will give you the desired contents in json_obj1;

0

精彩评论

暂无评论...
验证码 换一张
取 消