JSFiddle - React, Tailwind, and code Playground
by brigand
JavaScript
const data = [{
children: [],
},
{
children: [{
children: []
}],
},
{
children: [{
children: [{
children: []
}]
}],
},
{
children: [{
children: [],
children: [{
children: [{
children: []
}]
}]
}],
},
]
const getDepth = (c) => {
if (c.children && c.children.length) {
return 1 + Math.max(...c.children.map(getDepth));
}
return 0;
};
for (const [i, x] of data.entries()) {
console.log(i, getDepth(x))
}