I have a simple function looks at an object in an array. If it doesn't find a match based on a property it checks if it has children and then checks each of those for a property value.
It seems to work as expected through the first object with children, however it is not hitting that third layer.
The object is something like this:
data = [{
title: 'Home',
route: '/reports/home',
},
{
title: 'Layer 2',
children: [
{ title: 'Title 1', route: '/reports/title1' },
{ title: 'Title 2', route: '/reports/title2' },
],
},
{
title: 'Layer 3',
children: [
{ title: 'Title 3', route: '/reports/title3' },
{ title: 'Title 4', route: '/reports/title4' },
],
}];
lookUpTitle = navGroup => {
for (let item of navGroup) {
if (item.route && item.route.toLowerCase() === '/reports/title3') {
console.log(item.title)
return item.title;
} else {
if (item.children) {
return this.lookUpTitle(item.children);
}
}
}
};
lookUpTitle(data)
I'm simply calling the function and passing in the array as above.
I can find Title 2 just fine, but the function will not iterate through to the third object in the array if I'm looking for Title 3 or 4. What am I missing?
returnfrom a loop, the whole function will end.