I have a data where the key count appears 6 times and it is nested. I'm trying count the number of times it appears with below logic but somewhat I'm close to it not got the exact result.
The problem is child values I'm getting as 7 which I have consoled. but the final count is always 1 seriously I don't know why I'm getting 1. Any help!
let data = [{ "id": "1", "child": [ { "id": "12", "child": [ { "id": "123", "child": [ { "id": "1234" } ] } ] }, { "id": "2", "child": [ { "id": "22" } ] }, { "id": "3" }, { "id": "4", "child": [ { "id": "42", "child": [ { "id": "43" } ] } ] } ] }] const countChild = (arr,cnt = 0) => { for (const {child} of arr) { cnt = cnt + 1 console.log("child",cnt) if(child) countChild(child, cnt) } return cnt; }; console.log("Final count",countChild(data))