const componentTree = {
id: 1,
name: "App",
children: [
{
id: 2,
name: "Header",
children: [
{ id: 3, name: "Logo" },
{ id: 4, name: "Nav" }
]
},
{
id: 5,
name: "Content",
children: [
{ id: 6, name: "Sidebar" },
{
id: 7,
name: "Main",
children: [{ id: 8, name: "Article" }]
}
]
}
]
};
const bindUp = (obj, arr=[]) => {
if(obj.name) arr.push(obj.name)
if(obj.children){
obj.children.map(item=>{
bindUp(item, arr)
})
}
return arr
}
// const bindUp = (obj, arr = []) => {
// if (obj.name) arr.push(obj.name);
// if (obj.children) {
// obj.children.forEach(item => {
// bindUp(item, arr);
// });
// }
// return arr;
// };
console.log(bindUp(componentTree))
To embed this project on your website, copy the following code and paste it into your website's HTML: