简体   繁体   English

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

[英]How do I use the includes method in lodash to check if an object is in the collection?

lodash lets me check for membership of basic data types with includes : lodash 让我检查基本数据类型的成员资格, includes

_.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 ?如何使用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 更新

The includes (formerly called contains and include ) method compares objects by reference (or more precisely, with === ). includes (以前称为containsinclude )方法通过引用(或更准确地说,使用=== )比较对象。 Because the two object literals of {"b": 2} in your example represent different instances, they are not equal.因为您的示例中{"b": 2}的两个对象文字代表不同的实例,所以它们不相等。 Notice:注意:

({"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.另一方面, where (v4 中已弃用)和find方法通过对象的属性比较对象,因此它们不需要引用相等。 As an alternative to includes , you might want to try some (also aliased as any ):作为includes的替代方案,您可能想尝试some (也别名为any ):

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

Supplementing the answer by pswg , here are three other ways of achieving this using lodash 4.17.5 , without using _.includes() :通过pswg补充答案,这里是使用lodash 4.17.5实现此lodash其他三种方法,而不使用_.includes()

Say you want to add object entry to an array of objects numbers , only if entry does not exist already.说你要添加对象entry到对象的数组numbers ,只有entry不存在。

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:如果要返回Boolean ,在第一种情况下,您可以检查正在返回的索引:

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

You could use find to solve your problem您可以使用find来解决您的问题

https://lodash.com/docs/#find https://lodash.com/docs/#find

const data = [{"a": 1}, {"b": 2}]
const item = {"b": 2}


find(data, item)
// > true

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我如何使用 lodash 检查集合中的每个项目,除了那些不符合我的条件的项目? - How do I use lodash to check every item in a collection except those that dont meet my condition? 如何在对象中使用 .includes() 或类似内容? - How do I use .includes() or similar in a Object? 如何在 loDash 中使用 reduce 方法? 或者 Javascript 获取一组对象并创建一个对象 - How do I use the reduce method in loDash? Or Javascript Take a Array of Objects and Make one Object (Lodash)是否有一种方法可以检查集合中是否存在(嵌套)相同的对象? - (Lodash) Is there a method to check if an identical object exists (nested) in a collection? 如何检查数组是否包含Javascript中具有相同字段的对象? - How do I check if an array includes an object with the same field in Javascript? 如何使用lodash的._get方法检查path的2个值 - How to use lodash's ._get method to check for 2 values of path Lodash:当我嵌套Object时如何使用过滤器? - Lodash: how do I use filter when I have nested Object? 我想将 getElementByID 方法与 String Includes () 方法一起使用。 我怎样才能做到这一点? - I want to use the getElementByID method with the String Includes () method. How can I do that? 如何使用 Array includes() 方法检查数组中是否存在值? - How can i use Array includes() Method to check value exist in array? 如何使用数组包含并根据对象的属性检查对象 - How to use array includes and check an object against a property of an object
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM