0

The includes() function usually works pretty well, but with this specific case it always returns false and I have no clue why. I spent a ton of time making sure it wasn't just a typo, and I'm pretty sure it isn't.

I want my code to return true if the value given is in the array. But what it actually does is it returns false no matter what.

Here is the code:

let array = [
    {
        x: 10,
        y: 500,
        width: 100,
        height: 50
    },
    {
        x: 100,
        y: 550,
        width: 150,
        height: 20
    }
];


if (
    array.includes({
        x: 10,
        y: 500,
        width: 100,
        height: 50
    })
) {
    console.log(true);
} else {
    console.log(false);
}
Stae24
  • 1
  • 1

1 Answers1

2

Includes compares objects by reference. It does not do a deep comparison on each object. You probably need to use .find and create a function that will perform the deep comparison. lodash or a similar library will probably provide a function that will do this out of the box.

cementblocks
  • 4,326
  • 18
  • 24