I have a nested object like
{
name: "John",
parent:{
parent:{
parent:{
}
}
}
}
Now I want to get the level of the master parent object or basically how many times a parent object has been nested. In this case, I should be getting an output of 3
.
You could also do it recursively like this:
const obj = {
name: "John",
parent:{
parent:{
parent:{
parent:{
parent:{
parent:{
}
}
}
}
}
}
}
function findk(o,k,l=0){
if (o[k]) l=findk(o[k],k,++l)
return l
}
console.log(findk(obj,"parent"))