例如,我有以下 JSON 对象 json_obj1
json_obj1 = {x:{id:1,bars:{show:true,barWidth:0.4}}}
现在,我如何添加以下对象(使用 JavaScript):
y:{id:2,bars:{show:true,barWidth:0.4}}
到json_obj1
,这样它将是:
{x:{id:1,bars:{show:true,barWidth:0.4}},y:{id:2,bars:{show:true,barWidth:0.4}}}
最佳答案
您只需设置 json_obj1 的字段y即可
json_obj1 = {x:{id:1,bars:{show:true,barWidth:0.4}}}
json_obj1.y = {id:2,bars:{show:true,barWidth:0.4}}
现在 json_obj1 = {x:{id:1,bars:{show:true,barWidth:0.4}},y:{id:2,bars:{show:true,barWidth:0.4}}}
关于javascript 操作 json 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1943723/