I am struggling with checking an array for an array within. It doesn't seem that the include()
method is designed for it because it always returns false
in this case. Below is example of an identical scenario with indexOf
. Maybe all I need is syntax help, any ideas are most welcome.
arr = [1,[9,9],3,4,5];
if (arr.indexOf(el) !== -1) {
console.log(`the array contains ${el}`);
} else {
console.log(`doesn't contain ${el}`);
}
Of course the above returns true
for 1, 3, 4, 5 and false
for 2. And now the problem. I am trying to feed this method with an array like [9,9]
to see if there's one already.
let el = [9,9];
// console: "doesn't contain 9,9"
On the other hand the below is okay which makes me think it's just a syntax issue(?)
let el = arr[1];
// console: "the array contains 9,9"
I found a way around it by writing a checker function with for
loop but this quickly becomes bulky as you add requirements. I would love to know a smarter way.
Thank you.