基本上我有一个对象数组。每个对象都有一个我需要更改值的数组。
我正在使用React,所以这是一种状态:
<el-table
:data="packageForm.package_courses"
show-summary
:summary-method="getSummaries"
border>
>all the other stuff in there
<el-table>
更改此状态的[
{
"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
}
]
值的最佳方法是什么?我需要将它们乘以一个系数(4.96)。
答案 0 :(得分:12)
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)
答案 1 :(得分:2)
使用嵌套地图
const myData = [
{"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}
]
const newArray = myData.map(elm=>{
const points = elm.points.map(point=> point*4.96)
return {...elm , points}
})
console.log(newArray)
答案 2 :(得分:1)
const factor = 4.96
const arrayOfObject = [] // .. here your array of objects
const modifiedArrayOfObjects = arrayOfObject.map( stats => {
const newPoints = stats.points.map(point => point * factor)
stats.points = newPoints
return stats
}
在这里我制作了一个新的对象数组,在其中将每个对象映射到一个对象,其中每个点都乘以您的因子。