Home > Back-end >  Check if value is included in any nested array
Check if value is included in any nested array

Time:05-25

I am trying to find if an array of arrays includes a value. Is it possible to do this without a for loop?

The below always returns false.

const testArr = [
   [0,0,0],
   [0,1,0],
   [0,3,0]
]

console.log(testArr.includes(3))

CodePudding user response:

testArr.join().includes(String(3))

CodePudding user response:

You have to handle each level of arrays separately, whether that is by looping or by using array functions.

testArr.some(subArr => subArr.includes(3));

This will return true if one or more of the sub arrays include 3.

CodePudding user response:

testArr.reduce((a,b)=>{return [...a,...b]}).includes(3)
  • Related