问题描述
lodash让我用 code>和 include )方法通过引用(或者更确切地说,使用 === )比较对象。因为在您的示例中, {b:2} 的两个对象文字表示不同的实例,所以它们不相等。注意:
The includes (formerly called contains and include) method compares objects by reference (or more precisely, with ===). Because the two object literals of {"b": 2} in your example represent different instances, they are not equal. Notice:
({"b": 2} === {"b": 2}) > false
然而,这是可行的,因为只有一个 { b:2} :
However, this will work because there is only one instance of {"b": 2}:
var a = {"a": 1}, b = {"b": 2}; _.includes([a, b], b); > true
另一方面,(在v4中已弃用)和方法比较对象的属性,所以它们不需要引用相等。作为包含的替代方法,您可能需要尝试(也别名为任何):
On the other hand, the where(deprecated in v4) and find methods compare objects by their properties, so they don't require reference equality. As an alternative to includes, you might want to try some (also aliased as any):
_.some([{"a": 1}, {"b": 2}], {"b": 2}) > true
这篇关于如何在lodash中使用includes方法来检查对象是否在集合中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!