const arr = [
{
id: 0,
pid: 1,
name: 'tiger',
children: [
{
id: 5,
pid: 1,
name: 'small-tiger'
},
{
id: 6,
pid: 1,
name: 'big-tiger'
},
]
},
{
id: 1,
pid: 2,
name: 'fash',
children: [
{
id: 3,
pid: 2,
name: 'small-fish',
children: [
{
id: 3,
pid: 2,
name: 'small-fish-1',
},
{
id: 4,
pid: 2,
name: 'small-fish-2'
}
]
},
{
id: 4,
pid: 2,
name: 'big-fish'
},
]
}
]
// 递归算法 将多维数组转为一维数组
function recursionFunc(arr) {
let result = []
arr.forEach((item, index) => {
if (item.children && item.children.length > 0) {
result.push(...recursionFunc(item.children))
}
// Reflect.deleteProperty(obj, 'name')
delete item.children
result.push(item)
})
return result
}
const resultArr = recursionFunc(arr)
console.log(resultArr)
console