Рекурсивно обойти массив и сравнить значения с другим масивом
const a = [1, 2, 3]
const b = [4, 5, [1, 2, [3]], 4]
function isCompare(a, b) {
const rez = {}
b.forEach( i => {
rez[i] = Array.isArray(i) ? isCompare(a, i) : a === i
})
return rez
}
a.forEach(i => {
console.log(isCompare(i, b))
})
сейчас у меня получается объекты, не могу понять как сделать
1 === 1 rtue 1 === 2 false
Ответы (1 шт):
Автор решения: Igor
→ Ссылка
const a = [1, 2, 3, 6];
const b = [4, 5, [1, 2, [3]], 4];
function isCompare(a, b) {
let rez = false;
b.forEach(i => {
if (Array.isArray(i)) {
if (isCompare(a, i))
rez = true;
} else if (a === i) {
rez = true;
}
});
return rez;
}
a.forEach(i => {
console.log(i, isCompare(i, b));
});