vue----一维数组根据同一id改成二维数组
vue----一维数组根据同一id改成二维数组
初始数据(多个dimension_id值一样):
转换后的数据(类似于):
[
{
dimension_id: 'xxxxxxxxx',
desc: '111',
res: [
{ id: '4444444', self_score: 90 },
{ id: '5555555', self_score: 80 }
]
},
{
dimension_id: 'zzzzz',
desc: '111',
res: [
{ id: '1111', self_score: 70 },
{ id: '2222222', self_score: 60 }
]
}
]
代码转化:
let res = []
this.tableData.forEach((item) => {
let { dimension_id, self_explain, id, self_score } =
item
if (!res[dimension_id]) {
res[dimension_id] = {
dimension_id: dimension_id,
desc: self_explain || '',
res: []
}
}
res[dimension_id].res.push({
id: id,
self_score: self_score || ''
})
})
let result = Object.values(res)
打印效果: