Ben Carp
Ben Carp

Reputation: 26608

Arr.includes(item) - Can I Use with a Multidimensional Array?

I'm trying to use the arr.includes(item). The function should return True if the item is an element of the array. But it doesn't seem to be able to do so with a multidimensional array. Take a look at this screenshot (running node in the console):

enter image description here

I got a similar result on my Google Chrome.

Is it because it's an EC6 function, and not yet completely functional?

No information on such a problem on the Mozille page.

Upvotes: 2

Views: 1659

Answers (1)

Alnitak
Alnitak

Reputation: 339975

No, you can't use it on deep structures, because it performs an === test that checks that the operands are the same object, and not two (different) objects that happen to have the same contents.

On the MDN page you linked to there's a polyfill where you can see that === test within the sameValueZero() nested function.

For the above reasons, this would actually return true:

let a = [0, 1];
let b = [1, 2];
let c = [a, b];
c.includes(b);
> true

because the object passed to .includes really is the same object that's contained in c.

Upvotes: 6

Related Questions