I now have an array object, and there may or may not be a subset under each object in the array. At present, I have parsed the data using cyclic recursion. But now I don"t know how to add their own identity to each layer of the array, that is, if an item in the array does not have a subset, the level of the array is 1; If an item in the array has a subset, and the subset has a subset of its own, how to identify the level of this item? for specific code and screenshots, please see the description
below. `
let arr = [
{
title: "1",
href: "xxxxx",
children: []
},
{
title: "2",
href: "xxxxx",
children: [
{
title: "2",
href: "xxxxx",
children: []
},
{
title: "2",
href: "xxxxx",
children: []
},
{
title: "2",
href: "xxxxx",
children: [
{
title: "2",
href: "xxxxx",
children: []
},
{
title: "2",
href: "xxxxx",
children: []
},
{
title: "2",
href: "xxxxx",
children: []
}
]
}
]
},
{
title: "3",
href: "xxxxx",
children: []
}
]
arr.forEach (item = > {
)test(item)
})
function test (item) {
if (item.children.length) {
item.children.forEach(ele => {
ele.div = "<div>"+ele.title+"</div>"
test(ele)
})
} else {
item.div = "<div>"+item.title+"</div>"
}
}
console.log (arr)
`
as described above, can anyone give a solution or hint? Thank you