gpt4 book ai didi

javascript - JS : How to change the value of an object's property according to that object attribute

转载 作者:行者123 更新时间:2023-11-28 14:27:35 24 4
gpt4 key购买 nike

我有这样一个 JSON 数据:

inputData = [
{ id : 179849, name : alex , lastname: sanchez},
{ id : 788539, name : Paul, lastname: bearer},
{ id : 282169, name : Jean, lastname: nobel},
...
{ id : 632785, name : Maria, lastname: parak}
]

我需要根据id值搜索某个对象(例如:788539)

并更改其name属性的值 -> 名字变成:布鲁诺

数据将变成:

resultData= [
{ id : 179849, name : alex , lastname: sanchez},
{ id : 788539, name : bruno, lastname: bearer}, /* THIS ONE*/
{ id : 282169, name : Jean, lastname: nobel},
...
{ id : 632785, name : Maria, lastname: parak}
]

过滤/映射我的数据以进行此类修改的最简单方法是什么?

最佳答案

resultData= [
{ id : 179849, name : alex , lastname: sanchez},
{ id : 788539, name : bruno, lastname: bearer}, /* THIS ONE*/
{ id : 282169, name : Jean, lastname: nobel},
...
{ id : 632785, name : Maria, lastname: parak}
];

id = 788539;

resultData.forEach(ele=>{
if(ele.id == id){
ele.name = 'bruno'
}
});

console.log(resultData);

关于javascript - JS : How to change the value of an object's property according to that object attribute,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52633269/

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