如何使用下划线在 JavaScript 数组中获取重复项
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27854705/
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
How to get duplicates in a JavaScript Array using Underscore
提问by Abhishek Dutta
I have an array for which I need to the items that are duplicates and print the items based on a specific property. I know how to get the unique items using underscore.js but I need to find the duplicates instead of the unique values
我有一个数组,我需要它的重复项并根据特定属性打印这些项。我知道如何使用 underscore.js 获取唯一项,但我需要找到重复项而不是唯一值
var somevalue=[{name:"john",country:"spain"},{name:"jane",country:"spain"},{name:"john",country:"italy"},{name:"marry",country:"spain"}]
var uniqueList = _.uniq(somevalue, function (item) {
return item.name;
})
This returns:
这将返回:
[{name:"jane",country:"spain"},{name:"marry",country:"spain"}]
but I actually need the opposite
但我实际上需要相反的
[{name:"john",country:"spain"},{name:"john",country:"italy"}]
回答by chmac
A purely underscore based approach is:
纯粹基于下划线的方法是:
_.chain(somevalue).groupBy('name').filter(function(v){return v.length > 1}).flatten().value()
This would produce an array of all duplicate, so each duplicate will be in the output array as many times as it is duplicated. If you only want 1 copy of each duplicate, you can simply add a .uniq()
to the chain like so:
这将产生一个包含所有重复项的数组,因此每个重复项将在输出数组中出现的次数与它被复制的次数相同。如果您只想要每个副本的 1 个副本,您可以简单地将一个添加.uniq()
到链中,如下所示:
_.chain(somevalue).groupBy('name').filter(function(v){return v.length > 1}).uniq().value()
No idea how this performs, but I do love my one liners... :-)
不知道它的表现如何,但我确实喜欢我的一个衬垫...... :-)
回答by Igor Semin
Use .filter() and .where() for source array by values from uniq array and getting duplicate items.
根据 uniq 数组中的值使用 .filter() 和 .where() 作为源数组并获取重复项。
var uniqArr = _.uniq(somevalue, function (item) {
return item.name;
});
var dupArr = [];
somevalue.filter(function(item) {
var isDupValue = uniqArr.indexOf(item) == -1;
if (isDupValue)
{
dupArr = _.where(somevalue, { name: item.name });
}
});
console.log(dupArr);
UpdatedSecond way if you have more than one duplicate item, and more clean code.
如果您有多个重复项和更干净的代码,则更新第二种方式。
var dupArr = [];
var groupedByCount = _.countBy(somevalue, function (item) {
return item.name;
});
for (var name in groupedByCount) {
if (groupedByCount[name] > 1) {
_.where(somevalue, {
name: name
}).map(function (item) {
dupArr.push(item);
});
}
};
回答by Leo
Here how I have done the same thing:
在这里,我是如何做同样的事情的:
_.keys(_.pick(_.countBy(somevalue, b=> b.name), (value, key, object) => value > 1))
_.keys(_.pick(_.countBy(somevalue, b=> b.name), (value, key, object) => value > 1))
回答by user10522292
var somevalue=[{name:"john",country:"spain"},{name:"jane",country:"spain"},{name:"john",country:"italy"},{name:"marry",country:"spain"}];
var uniqueList = _.uniq(somevalue, function (item) {return item.country;})
//from this you will get the required output
//由此您将获得所需的输出