一个煎蛋的js树形结构,没有html,因为我不会
const getList = [
{ id: 1, name: null, team: 'sb1' },
{ id: 11, name: 1, team: 'sb-1' },
{ id: 12, name: 1, team: 'sb-2' },
{ id: 2, name: null, team: 'sb2' },
{ id: 21, name: 2, team: 'sb-3' },
{ id: 22, name: 2, team: 'sb-4' },
{ id: 3, name: null, team: 'sb3' },
{ id: 31, name: 3, team: 'sb-5' },
{ id: 32, name: 3, team: 'sb-6' }
]
const treeList = ((getList, id, list) => {
for (let item of getList) {
if (item.name == id) {
list.push(item)
}
}
for (let i of list) {
i.children = []
treeList(getList, i.id, i.children)
if (i.children.length == 0) {
delete i.children
}
}
// console.log('list', list);
return list
})
// console.log('list', list);
const res = treeList(getList, null, [])
console.log('res', res);