let item = {
id: '1',
children: [
{ id: '2', children: [{ id: '2-1' ,children:[{id: '2-1-1',children:[{id: '2-1-1-1',children:[]}]}]}]},
{ id: '3', children: [{ id: '3-1' ,children:[]}]},
{ id: '4', children: [{ id: '4-1' ,children:[]}]},
]
};
const flattenItem = (item, currentDeep = 1, depth) => {
item.deep = currentDeep;
if (item.children && currentDeep !== depth) {
return item.children.map((child, index) => flattenItem(child, currentDeep + 1, depth)).flat();
} else {
return [item];
}
}
console.log(flattenItem(item,1,6));
封装一个函数,获取对象的特定层级的数据。
最新推荐文章于 2022-06-16 14:42:58 发布