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
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
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]
}))