const data = [{
id: 1,
name: 'a',
children: [{
id: 12,
name: 'aa',
children: [{
id: 121,
name: 'aaa'
},
{
id: 122,
name: 'aab'
}
]
}]
},
{
id: 2,
name: 'b',
children: [{
id: 22,
name: 'ba',
children: [{
id: 221,
name: 'baa'
},
{
id: 222,
name: 'bab'
}
]
}]
}
]
const result = data.reduce(function (prev, curr) {
prev.push({
value: curr.id,
lable: curr.name,
parentId: curr.parentId
})
curr.children && curr.children.forEach(v => {
v.parentId = curr.id
arguments.callee(prev, v)
})
return prev
}, [])
扁平化数组对象
于 2023-08-28 23:25:36 首次发布