[英]Set.prototype.has() with predicate
var myset = new Set(); myset.add({ key: 123, value: 100 }); var has = myset.has({ key: 123, value: 100 }); console.log(has); // false var obj = { key: 456, value: 200 }; myset.add(obj); has = myset.has(obj); console.log(has); // true has = myset.has(x => x.key === 123); console.log(has); // false
The problem in this case: I just add { key: 123, value: 100 }
to myset
, why doesn't it contain { key: 123, value: 100 }
? 在这种情况下的问题:我只是将
{ key: 123, value: 100 }
到myset
,为什么它不包含{ key: 123, value: 100 }
?
Another case, if I use obj
instead of { key: 123, value: 100 }
, it would return true
. 另一种情况,如果我使用
obj
而不是{ key: 123, value: 100 }
,它将返回true
。
Set.prototype.has() says: Set.prototype.has()说:
The has() method returns a boolean indicating whether an element with the specified value exists in a Set object or not.
has()方法返回一个布尔值,指示具有指定值的元素是否存在于Set对象中。
But that doesn't mention about: what's specified value
? 但这没有提到:什么是
specified value
?
Clearly, in this case { key: 123, value: 100 }
and { key: 123, value: 100 }
are similar to, and.... I'm getting false
. 显然,在这种情况下,
{ key: 123, value: 100 }
和{ key: 123, value: 100 }
类似,并且......我变得false
。 So what's specified
here? 那么这里
specified
了什么?
And the second question: why doesn't they support predicate
in has()
method? 第二个问题:为什么它们不支持
has()
方法中的predicate
?
In my example. 在我的例子中。 It's harder to search if I use
for...of...
: 如果我使用
for...of...
来搜索更难:
for (let obj of myset) {
if (obj.key === 123) return true;
}
While it can be inline with predicating: 虽然它可以与预测内联:
has = myset.has(x => x.key === 123)
So, should it be improved for future? 那么,未来应该改进吗?
{ key: 123, value: 100 } === { key: 123, value: 100 }
is false
because JavaScript performs a shallow comparison. { key: 123, value: 100 } === { key: 123, value: 100 }
为false
因为JavaScript执行浅层比较。 Each object literal creates a new object, they may hold the same values, but they are still different objects that just happens to look alike. 每个对象文字创建一个新对象,它们可以保持相同的值,但它们仍然是恰好相似的不同对象。
var a = {};
var b = a;
a === b; // true
In this example you get true
because now you are comparing the same object. 在这个例子中,你得到了
true
因为现在你正在比较同一个对象。 You can tell that a
, and b
are the same object because changes in a
are reflected in b
. 你可以告诉大家
a
和b
是相同的对象,因为在改变a
反映在b
。
a.x = 1;
b.x === 1; // true
myset.has(x => x.key === 123)
here you are asking if the set has this new lambda that you just created. myset.has(x => x.key === 123)
在这里你要问的是这个集合是否有你刚刚创建的这个新lambda。 It would be nice if has
used your lambda to check the elements of the set, but unfortunately the method does perform this check. 如果这将是不错
has
用你的lambda来检查集合中的元素,但不幸的是,方法不执行此检查。
Set.prototype.has
doesn't find the object because it tests using value equality , meaning: Set.prototype.has
找不到对象,因为它使用值相等进行测试,这意味着:
{ key: 123 } !== { key: 123 } // true
If you want to be able to find an item based on a predicate, you will have to add that function manually. 如果您希望能够根据谓词查找项目,则必须手动添加该功能。 The function probably doesn't exist already because a
Set
is only efficient when you need fast lookups without iteration. 该函数可能不存在,因为只有在需要快速查找而没有迭代时,
Set
才有效。 If you want to iterate values, use an array instead. 如果要迭代值,请改用数组。
Here's how you could implement it just as Array.prototype.find
: 以下是如何像
Array.prototype.find
一样实现它:
Set.prototype.find = function () {
return Array.prototype.find.apply([...this], arguments);
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.