I have recursive function which looks thru deeply nested object or array of ojbects and finds wanted object by key. Problem is when i want to return result object i get undefined and i dont know why. When i console log result i get correct result.
function checkForDependency(name, scope) {
if (Array.isArray(scope)) {
scope.forEach((el) => {
return checkForDependency(name, el)
})
} else if (typeof scope === 'object') {
if (scope.hasOwnProperty('name') && scope.name == name && scope.hasOwnProperty('dependency')) {
console.log('dependency:', scope.dependency)
return {
type: scope.dependency
}
} else {
for (let key in scope) {
if (Array.isArray(scope[key]) || typeof scope[key] === 'object') {
return checkForDependency(name, scope[key])
}
}
}
}
}
Could you help me please?
<>and create a minimal reproducible exampleiffollowed by anelse if...where is theelseblock. Same goes for inside yourelse ifblock, thefor-loophas nothing after it. If this is a recursive function, you cannot afford to leave any path way untouched, and this is probably why you are getting undefined at times