var myset = new Set();
myset.add({ key: 123, value: 100 });
var has = myset.has({ key: 123, value: 100 });
console.log(has);
var obj = {
key: 456,
value: 200
};
myset.add(obj);
has = myset.has(obj);
console.log(has);
has = myset.has(x => x.key === 123);
console.log(has);
Run codeHide resultThe problem in this case: I just add { key: 123, value: 100 }in myset, why doesn't it contain { key: 123, value: 100 }?
Another case, if I use objinstead { key: 123, value: 100 }, it will return true.
Set.prototype.has () says:
The has () method returns a boolean value indicating whether an element with the specified value exists in the Set object or not.
But this does not mean that specified value:?
It is clear that in this case { key: 123, value: 100 }both are { key: 123, value: 100 }similar, and .... I get false. So what is specifiedhere?
And the second question: why don't they support the method predicatein has()?
In my example. It is harder to search if I use for...of...:
for (let obj of myset) {
if (obj.key === 123) return true;
}
While it can be built in with prediction:
has = myset.has(x => x.key === 123)
, ?