I created a Set of 2D arrays like this:
let set = new Set([[1, 2], [4, 6]]);
now when I am doing set.has([1, 2])
it returns false
.
How can I achieve this?
I created a Set of 2D arrays like this:
let set = new Set([[1, 2], [4, 6]]);
now when I am doing set.has([1, 2])
it returns false
.
How can I achieve this?
If you're going to compare objects or arrays (which aren't primitives, and so aren't ===
), stringify everything in the Set first:
const set = new Set([[1, 2], [4, 6]]);
const setStringified = new Set([...set].map(JSON.stringify));
console.log(setStringified.has(JSON.stringify([1, 2])));