JSFiddle - React, Tailwind, and code Playground
by vaheed akhtar
JavaScript
let count = 0;
const arr = [{
id: 0, children: []
}, {
id: 1, children: [{
id: 2, children: []
}, {
id: 3, children: [{
id: 4, children: []
}]
}]
}];
const assignDepth = (arr, depth = 0, index = 0) => {
if(index < arr.length){
arr[index].depth = depth;
if(arr[index].children.length){
count++;
return assignDepth(arr[index].children, depth+1, 0);
};
count++;
return assignDepth(arr, depth, index+1);
};
return;
};
assignDepth(arr);
console.log(count);