3 回答

TA貢獻(xiàn)1794條經(jīng)驗(yàn) 獲得超8個(gè)贊
You can have few different approach to achieve this
1. using Map
let tmp = [
{'id' : 'a', arr : [0,0,1]},
{'id' : 'b', arr : [1,0,1]},
{'id' : 'c', arr : [1,4,1]}
]
var updated_array;
updated_array = tmp.map(function(element){
if(element.id == 'a'){
return {
id: element.id,
arr : [3,0,1]
}
} else {
return element
}
})
2. Using Filter
updated_array = tmp.filter(function(element){
if(element.id == 'a'){
return {
id: element.id,
arr : [3,0,1]
}
} else {
return element
}
})
3. Using ForEach
tmp.forEach(function(element){
if(element.id == 'a'){
element.arr = [3,0,1]
}
})
4. You can also use traditional while or for loop to achieve this

TA貢獻(xiàn)1865條經(jīng)驗(yàn) 獲得超7個(gè)贊
像這樣嘗試
let value =[
{'id' : 'a', arr : [0,0,1]},
{'id' : 'b', arr : [1,0,1]},
{'id' : 'c', arr : [1,4,1]}
]
value.forEach(val=>{
if(val.id=='a'){
val.arr=[3,0,1]
}
})

TA貢獻(xiàn)1811條經(jīng)驗(yàn) 獲得超6個(gè)贊
您可以使用數(shù)組的 map() 并更新值。檢查下面的代碼。
let tmp = [
{'id' : 'a', arr : [0,0,1]},
{'id' : 'b', arr : [1,0,1]},
{'id' : 'c', arr : [1,4,1]}
]
tmp.map(item =>{
if (item.id === 'a'){
item.arr=[3,0,1];
}
})
console.log(tmp);
添加回答
舉報(bào)