是否可以向现有JSON数据添加新属性?

时间:2016-02-22 16:58:16

标签: javascript arrays json

我有以下JSON数据:

$scope.users = [{ "id": 1, "first_name": "Philip",  "country": "Indonesia" },
                { "id": 2, "first_name": "Judith", "country": "China" },
                { "id": 3, "first_name": "Julie",  "country": "Finland"},
                { "id": 4, "first_name": "Gloria",  "country": "Indonesia"}}];

我想添加一个像这样的新属性:

$scope.users = [{ "id": 1, "first_name": "Philip",  "country": "Indonesia", "new_field":4 },
                { "id": 2, "first_name": "Judith", "country": "China","new_field":4 },
                { "id": 3, "first_name": "Julie",  "country": "Finland","new_field":4},
                { "id": 4, "first_name": "Gloria",  "country": "Indonesia","new_field":4}}];

这可能吗?如果是,怎么办呢?

1 个答案:

答案 0 :(得分:3)

使用此:

for(var i=0;i<$scope.users.length;i++)
    $scope.users[i].new_field =4;