user4018884
user4018884

Reputation:

How to check if a set contains an array in Javascript?

I would like to check if a set contains an array.

var x = new Set();
x.add(2);
x.has(2); //returns true, OK
x.add([3,4]);
x.has([3,4]); //returns false

I know this:

[1,2] == [1,2]; //returns false

I can compare two arrays using loop or JSON.stringify. However how to deal with it in case of arrays inside a set?

Upvotes: 0

Views: 2235

Answers (1)

Logan Murphy
Logan Murphy

Reputation: 6230

Sets essentially use === for comparison of values stored in the set. If you want to solve this yourself you will have to resort to looping or creating your own set data structure. When you make your solution you can make it dependent on an equals method so that each object can independently determine if it is equal to another.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set

Upvotes: 1

Related Questions