0

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))
mellows
  • 303
  • 1
  • 7
  • 27
  • [`testArr.some(subArr => subArr.includes(3))`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/some) – CRice May 24 '22 at 18:26

3 Answers3

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

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.

James
  • 20,957
  • 5
  • 26
  • 41
0
testArr.reduce((a,b)=>{return [...a,...b]}).includes(3)
Lee
  • 29,398
  • 28
  • 117
  • 170
  • Please don't post only code as answer, but also provide an explanation what your code does and how it solves the problem of the question. Answers with an explanation are usually more helpful and of better quality, and are more likely to attract upvotes. – Mark Rotteveel May 25 '22 at 11:21