I can add into Set exactly the same arrays, but Set mustn't add elements that already exist in this Set. Set must store only unique values. But Why am I able to add these?
let s = new Set();
s.add([1]);
s.add([1]);
s.add([1]);
console.log(s); //Set(3) {Array(1), Array(1), Array(1)}
CodePudding user response:
The problem with the Set
object in ES6 is that the equality check is based on comparing the actual reference of inserted objects. Thus, when inserting multiple Arrays with the exact same content, it treats them as non-identical as the underlying object reference differs. Same goes for other objects. Primitives on the other hand are compared based on their values.
CodePudding user response:
If you want to add the list/array itself and not its members, then you must use a tuple, unfortunately. Set members must be hashable.
Link: Add list to set?