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
Lodash remove duplicates from array
提问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
_.unique
no 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 _.uniqBy
either 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 id
and your data is stored in data
variable, 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
回答by Vixson
回答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