这是我的数据:
[
{
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"
}
]
使用Lodash,我如何删除具有重复id键的对象?带有过滤器、地图和唯一性的东西,但不是很确定。
我的真实数据集要大得多,并且有更多的键,但概念应该是相同的。
发布于 2016-11-18 19:33:28
您可以使用lodash方法_.uniqWith,它在当前版本的lodash 4.17.2中可用。
示例:
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 }]
发布于 2019-03-24 03:55:58
或者简单地使用union,对于简单的数组。
_.union([1,2,3,3], [3,5])
// [1,2,3,5]
发布于 2019-11-07 05:06:50
使用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
来源:https://www.codegrepper.com/?search_term=Lodash+remove+duplicates+from+array
https://stackoverflow.com/questions/31740155
复制相似问题