0
var coords = [
    [0,0],[5,0],[0,5],[5,5],[8,5],[8,0],[40,5],[5,54],[6,7],[40,0],[8,54]
]
console.log(coords.includes([0,0]))

The result here is false, and I am unsure why. Please help

Owen Moogk
  • 37
  • 8
  • [Duplicate](https://google.com/search?q=site%3Astackoverflow.com+js+includes+with+nested+array+returns+false) of [Why can't I use Array#includes for a nested array?](https://stackoverflow.com/q/48088657/4642212). – Sebastian Simon Jan 17 '21 at 21:22

1 Answers1

1

Array.prototype.includes compares the values using the strictly equal operator. Arrays are stored by reference and not by value. [] === [] will never be true, unless you are comparing the same array stored in your array.

A way to solve this issue is using Array.prototype.some

var coords = [
    [0,0],[5,0],[0,5],[5,5],[8,5],[8,0],[40,5],[5,54],[6,7],[40,0],[8,54]
]
console.log(coords.some(coordinate => {
const [x, y] = coordinate
// This is the value you are comparing
const myCoords = [0, 0]
return x === myCoords[0] && y === myCoords[1]
}))

Uzair Ashraf
  • 1,171
  • 8
  • 20