gpt4 book ai didi

javascript - 以编程方式添加数组的对象属性

转载 作者:行者123 更新时间:2023-11-30 08:44:17 25 4
gpt4 key购买 nike

[
{
"uId": "2",
"tabId": 1,
"tabName": "Main",
"points": "10"
},
{
"uId": "3",
"tabId": 2,
"tabName": "Photography",
"points": "20"
}
]

如何通过检查其属性值来插入指定数组?说我想在 uId = 3 中添加一个 assoc 对象,我该怎么做?还是技术上不可能?

最佳答案

这也可以使用 array.map (第 5 版中添加到 ECMA-262 标准):

array.map(function(i){
if(i.uId == 3) i['newprop'] = 'newValue';
});

Example Here .

更新:它可以是一个数组

if(i.uId == 3) i['newprop'] = ['newvalue1', 'newvalue2'];

Example2 Here.

关于javascript - 以编程方式添加数组的对象属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23306380/

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