Javascript Lodash 从数组中删除重复项

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31740155/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 07:06:49  来源:igfitidea点击:

Lodash remove duplicates from array

javascriptlodash

提问by ChrisRich

This is my data:

这是我的数据:

[
    {
        url: 'www.example.com/hello',
        id: "22"    
    },
    {
        url: 'www.example.com/hello',
        id: "22"    
    },
    {
        url: 'www.example.com/hello-how-are-you',
        id: "23"    
    },
    {
        url: 'www.example.com/i-like-cats',
        id: "24"    
    },
    {
        url: 'www.example.com/i-like-pie',
        id: "25"    
    }
]

With Lodash, how could I remove objects with duplicate id keys? Something with filter, map and unique, but not quite sure.

使用 Lodash,如何删除具有重复 id 键的对象?带有过滤器,地图和独特的东西,但不太确定。

My real data set is much larger and has more keys, but the concept should be the same.

我的真实数据集要大得多,键也更多,但概念应该是一样的。

回答by ntalbs

_.uniqueno longer work for the current version as lodash 4.0.0 has this breaking change. The functionally was splitted into _.uniq, _.sortedUniq, _.sortedUniqBy, & _.uniqBy

_.unique不再适用于当前版本,因为 lodash 4.0.0 有这个重大变化。功能上分为_.uniq, _.sortedUniq, _.sortedUniqBy, & _.uniqBy

You could use _.uniqByeither by

你可以使用_.uniqBy无论是

_.uniqBy(data, function (e) {
  return e.id;
});

or

或者

_.uniqBy(data, 'id');

Documentation: https://lodash.com/docs#uniqBy

文档:https: //lodash.com/docs#uniqBy



For older versions of lodash( < 4.0.0 )

对于旧版本的 lodash( < 4.0.0 )

Assuming that the data should be unique by idand your data is stored in datavariable, you can use unique()function like this:

假设数据应该是唯一的id并且您的数据存储在data变量中,您可以像这样使用unique()函数:

_.unique(data, function (e) {
  return e.id;
});

Or simply

或者干脆

_.uniq(data, 'id');

回答by zdrsoft

You could use lodash method _.uniqWith, it is available in the current version of lodash 4.17.2.

您可以使用 lodash 方法 _.uniqWith,它在 lodash 4.17.2 的当前版本中可用。

Example:

例子:

var objects = [{ 'x': 1, 'y': 2 }, { 'x': 2, 'y': 1 }, { 'x': 1, 'y': 2 }];

_.uniqWith(objects, _.isEqual);
// => [{ 'x': 1, 'y': 2 }, { 'x': 2, 'y': 1 }]

More info: https://lodash.com/docs/#uniqWith

更多信息:https: //lodash.com/docs/#uniqWith

回答by Vixson

Or simply Use union, for simple array.

或者干脆使用union,用于简单数组。

_.union([1,2,3,3], [3,5])

// [1,2,3,5]

回答by Hassan Ajaz

Simply use _.uniqBy(). It creates duplicate-free version of an array.

只需使用_.uniqBy()。它创建了一个数组的无重复版本。

This is a new way and available from 4.0.0 version.

这是一种新方法,可从 4.0.0 版本开始使用。

_.uniqBy(data, 'id');

or

或者

_.uniqBy(data, obj => obj.id);

回答by Taylor Hawkes

With lodash version 4+, you would remove duplicate objects either by specific property or by the entire object like so:

使用 lodash 版本 4+,您可以通过特定属性或整个对象删除重复对象,如下所示:

var users = [
  {id:1,name:'ted'},
  {id:1,name:'ted'},
  {id:1,name:'bob'},
  {id:3,name:'sara'}
];
var uniqueUsersByID = _.uniqBy(users,'id'); //removed if had duplicate id
var uniqueUsers = _.uniqWith(users, _.isEqual);//removed complete duplicates

Source:https://www.codegrepper.com/?search_term=Lodash+remove+duplicates+from+array

来源:https: //www.codegrepper.com/?search_term=Lodash+remove+duplicates+from+array

回答by Himanshu Tanwar

You can also use unionByfor 4.0.0 and later, as follows: let uniques = _.unionBy(data, 'id')

您还可以将unionBy用于 4.0.0 及更高版本,如下所示: let uniques = _.unionBy(data, 'id')