gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 18:44:43 25 4
gpt4 key购买 nike

我有以下 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}}];

这可能吗?如果是,如何做到这一点?

最佳答案

使用这个:

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

关于javascript - 是否可以向现有 JSON 数据添加新属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35559427/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com