How to identify each layer of a set of array data

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

May.14,2021

depending on your requirements, it would be better to use recursion, but this tail recursion is difficult to optimize
because this similar requirement must first get the deepest node. In order to avoid some repeated calculations, it is difficult to make tail recursion, because the information of the last call stack needs to be stored. Get the result after the execution of the internal function and then assign it:

func(data)

console.log(data)
  
function func(data) {
  data.forEach(item => {
    item.depth = getDepth(item)
  })
}

function getDepth(data) {
  return data.children.length ? Math.max(...data.children.map(child => {
    child.depth = getDepth(child)
    return (child.depth || 0) + 1
  })) : 1
}

P.S. These two functions can be merged into one, leaving it to you to practice

.
MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1ea34e2-47a0c.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1ea34e2-47a0c.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?