基本上我有一个对象数组。每个对象都有一个数组,我需要更改它的值。
我正在使用 React,所以这是一个状态:
[
{
"points": [
60,
5,
60,
20,
70,
20,
70,
15
],
"finished": true,
"color": "#6292C6"
},
{
"points": [
80,
15,
80,
25,
90,
25
],
"finished": true,
"color": "#3FD971"
},
{
"cultureName": "",
"points": [],
"finished": false
}
]
更改此状态的 points
值的最佳方法是什么?我需要将它们乘以一个因子 (4.96)。
最佳答案
map
你的数组,spread
它里面的每个对象只覆盖属性 points
(map
乘以每个项目因数 4.96
)
const data = [{id: 1, points: [1,2,3]}, {id: 2, points: []}]
const changedData = data.map(item =>({
...item,
points : item.points.map(value => value * 4.96)
}))
console.log(changedData)
关于javascript - 如何更改对象数组中数组中的每个值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58562864/