如何在lodash中使用includes方法来检查对象是否在集合中?

时间:2022-12-06 07:31:38

lodash lets me check for membership of basic data types with includes:

lodash让我检查基本数据类型的成员资格,包括:

_.includes([1, 2, 3], 2)
> true

But the following doesn't work:

但以下不起作用:

_.includes([{"a": 1}, {"b": 2}], {"b": 2})
> false

This confuses me because the following methods that search through a collection seem to do just fine:

这让我感到困惑,因为搜索集合的以下方法似乎做得很好:

_.where([{"a": 1}, {"b": 2}], {"b": 2})
> {"b": 2}
_.find([{"a": 1}, {"b": 2}], {"b": 2})
> {"b": 2}

What am I doing wrong? How do I check for the membership of an object in a collection with includes ?

我究竟做错了什么?如何检查包含包含的集合中对象的成员身份?

edit: question was originally for for lodash version 2.4.1, updated for lodash 4.0.0

编辑:问题最初是针对lodash版本2.4.1,更新为lodash 4.0.0

2 个解决方案

#1


137  

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:

包含(以前称为contains和include)方法通过引用(或更确切地说,使用===)来比较对象。因为示例中的{“b”:2}的两个对象文字表示不同的实例,所以它们不相等。注意:

({"b": 2} === {"b": 2})
> false

However, this will work because there is only one instance of {"b": 2}:

但是,这将起作用,因为只有一个{“b”:2}的实例:

var a = {"a": 1}, b = {"b": 2};
_.includes([a, b], b);
> true

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):

另一方面,where(在v4中弃用)和find方法按对象的属性比较对象,因此它们不需要引用相等。作为包含的替代方法,您可能想尝试一些(也有任何别名):

_.some([{"a": 1}, {"b": 2}], {"b": 2})
> true

#2


2  

Supplementing the answer by p.s.w.g, here are three other ways of achieving this using lodash 4.17.5, without using _.includes():

通过p.s.w.g补充答案,以下是使用lodash 4.17.5实现此目的的另外三种方法,不使用_.includes():

Say you want to add object entry to an array of objects numbers, only if entry does not exist already.

假设您想要将对象条目添加到对象数组中,只有在条目不存在的情况下才会添加。

let numbers = [
    { to: 1, from: 2 },
    { to: 3, from: 4 },
    { to: 5, from: 6 },
    { to: 7, from: 8 },
    { to: 1, from: 2 } // intentionally added duplicate
];

let entry = { to: 1, from: 2 };

/* 
 * 1. This will return the *index of the first* element that matches:
 */
_.findIndex(numbers, (o) => { return _.isMatch(o, entry) });
// output: 0


/* 
 * 2. This will return the entry that matches. Even if the entry exists
 *    multiple time, it is only returned once.
 */
_.find(numbers, (o) => { return _.isMatch(o, entry) });
// output: {to: 1, from: 2}


/* 
 * 3. This will return an array of objects containing all the matches.
 *    If an entry exists multiple times, if is returned multiple times.
 */
_.filter(numbers, _.matches(entry));
// output: [{to: 1, from: 2}, {to: 1, from: 2}]

If you want to return a Boolean, in the first case, you can check the index that is being returned:

如果要返回布尔值,在第一种情况下,可以检查要返回的索引:

_.findIndex(numbers, (o) => { return _.isMatch(o, entry) }) > -1;
// output: true

#1


137  

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:

包含(以前称为contains和include)方法通过引用(或更确切地说,使用===)来比较对象。因为示例中的{“b”:2}的两个对象文字表示不同的实例,所以它们不相等。注意:

({"b": 2} === {"b": 2})
> false

However, this will work because there is only one instance of {"b": 2}:

但是,这将起作用,因为只有一个{“b”:2}的实例:

var a = {"a": 1}, b = {"b": 2};
_.includes([a, b], b);
> true

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):

另一方面,where(在v4中弃用)和find方法按对象的属性比较对象,因此它们不需要引用相等。作为包含的替代方法,您可能想尝试一些(也有任何别名):

_.some([{"a": 1}, {"b": 2}], {"b": 2})
> true

#2


2  

Supplementing the answer by p.s.w.g, here are three other ways of achieving this using lodash 4.17.5, without using _.includes():

通过p.s.w.g补充答案,以下是使用lodash 4.17.5实现此目的的另外三种方法,不使用_.includes():

Say you want to add object entry to an array of objects numbers, only if entry does not exist already.

假设您想要将对象条目添加到对象数组中,只有在条目不存在的情况下才会添加。

let numbers = [
    { to: 1, from: 2 },
    { to: 3, from: 4 },
    { to: 5, from: 6 },
    { to: 7, from: 8 },
    { to: 1, from: 2 } // intentionally added duplicate
];

let entry = { to: 1, from: 2 };

/* 
 * 1. This will return the *index of the first* element that matches:
 */
_.findIndex(numbers, (o) => { return _.isMatch(o, entry) });
// output: 0


/* 
 * 2. This will return the entry that matches. Even if the entry exists
 *    multiple time, it is only returned once.
 */
_.find(numbers, (o) => { return _.isMatch(o, entry) });
// output: {to: 1, from: 2}


/* 
 * 3. This will return an array of objects containing all the matches.
 *    If an entry exists multiple times, if is returned multiple times.
 */
_.filter(numbers, _.matches(entry));
// output: [{to: 1, from: 2}, {to: 1, from: 2}]

If you want to return a Boolean, in the first case, you can check the index that is being returned:

如果要返回布尔值,在第一种情况下,可以检查要返回的索引:

_.findIndex(numbers, (o) => { return _.isMatch(o, entry) }) > -1;
// output: true