Home > Enterprise >  How to check if subarray (within array) matches a value?
How to check if subarray (within array) matches a value?

Time:09-27

So i have some code that tries to detect if a block (on a canvas) is hitting another block by checking that current blocks x coordinates and seeing if it matches any other blocks x coordinates. The problem is, i cant use array.includes() because it will also check all of the y values too. The structure is someting like this:

array = [
[x, y],
[x, y],
]

etc...

is there any way to only check subarrays of that array?

CodePudding user response:

Check if .some of the subarrays have an item at the 1st index that's the same.

const collision = array.some(subarr => subarr[0] === xToCheck);
  • Related