Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading