почему возвращаю return true если найден а в результате все равно false? vue.js
data() {
return {
relatives: {
main: null,
spouse: 30,
childrens: {
child1: null,
child2: null,
child3: null,
child4: 15,
child5: null,
},
parents: {
parent1: 50,
parent2: null,
parent3: null,
parent4: null,
},
}
}
}
methods: {
exists_person(relatives) {
for (const [key, value] of Object.entries(relatives)) {
if (typeof value === 'object' && value !== null) {
this.exists_person(value);
} else {
if (relatives[key] >= 18) {
console.log(relatives[key]);
return true; //вот это почему то не возвращает как результат всей функции хотя совпадения соответствуют
}
}
}
return false;
},
}
if (this.exists_person(this.relatives)) {
console.log('true');
} else {
console.log('false');
}
Ответы (1 шт):
Автор решения: Эникейщик
→ Ссылка
Если выполнение попадает сюда:
if (typeof value === 'object' && value !== null) {
this.exists_person(value);
то функция ничего не вернет. Т.е. вернет, но не туда, где ждут.
Замени на
return this.exists_person(value);