如何在javascript

时间:2016-06-29 03:39:51

标签: javascript angularjs json

这是我目前的json回复。

[{"id":1,"name":"Yangon","$$hashKey":"object:170"}]

但我想在json上面添加新节点,如下所示,

[{"id":1,"name":"Yangon","$$hashKey":"object:170", "township_id": 1, "township_name": "Ahlone"}]

请帮我怎么做。提前谢谢。

3 个答案:

答案 0 :(得分:1)

angular.extend是另一种方法。

var arr = [{ "id": 1, "name": "Yangon", "$$hashKey": "object:170" }];
arr[0] = angular.extend(arr[0], {
    "township_id": 1,
    "township_name": "Ahlone"
});

答案 1 :(得分:1)

var arrayofobjects=[{"id":1,"name":"Yangon","$$hashKey":"object:170"}]
arrayofbojects[0].township_id: 1;
arrayofbojects[0].township_name: "Ahlone";

console.log(arrayobjectg[0])

试试这是经过测试的代码。

答案 2 :(得分:0)

您需要将响应存储到变量中(可能已经存在),只需添加您想要的任何键/值:

示例

var arr = [{ "id": 1, "name": "Yangon", "$$hashKey": "object:170" }];

arr[0]['township_id'] = 1;
arr[0]['township_name'] = 'Ahlone';

<强>结果

[{"id":1,"name":"Yangon","$$hashKey":"object:170","township_id":1,"township_name":"Ahlone"}]