reduce写法
最简洁的写法
function flat(tree) {
if (!tree || tree.length === 0) return acc
return tree.reduce((acc, cur) => {
return acc.concat(cur, cur.children && cur.children.length ? flat(cur.children) : [])
}, [])
}
forEach
const resultArr = [];
function flat(tree) {
if (!tree || tree.length === 0) return [];
tree.forEach((node) => {
resultArr.push({ ...node });
return flat(node.children);
});
}