如何在Javascript对象下添加新的键值对?

时间:2017-01-20 06:05:51

标签: javascript jquery

[{"name":"Top Level","parent":"null"}]

必需的输出:

[{
"name": "Top Level",
"parent": "null",
"children": [
{
  "New_Key":"New_value"
}
]
}]

如何在"New_Key":"New_value"密钥下添加"parent"对?

2 个答案:

答案 0 :(得分:-1)

非常简单,你可以添加任何新项目,如obj [“key”] = value,在你的情况下你可以像下面这样做

{{1}}

答案 1 :(得分:-1)

var arr = [{"name":"Top Level","parent":"null"}];
for(var i=0; i<arr.length; i++){
    arr[i].children = [{
                        "New_Key":"New_value"
                      }];
}