Javascript 使用 lodash 检查数组是否有重复值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28461014/
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
Using lodash to check whether an array has duplicate values
提问by nackjicholson
What do you all think would be the best (best can be interpreted as most readable or most performant, your choice) way to write a function using the lodash utilities in order to check an array for duplicate values.
你们都认为什么是使用 lodash 实用程序编写函数以检查数组是否有重复值的最佳方式(最好可以解释为最易读或性能最高,您的选择)。
I want to input ['foo', 'foo', 'bar']and have the function return true. And input ['foo', 'bar', 'baz']and have the function return false.
我想输入['foo', 'foo', 'bar']并让函数返回true。并输入['foo', 'bar', 'baz']并具有函数 return false。
回答by agershun
You can try this code:
你可以试试这个代码:
function hasDuplicates(a) {
return _.uniq(a).length !== a.length;
}
var a = [1,2,1,3,4,5];
var b = [1,2,3,4,5,6];
document.write(hasDuplicates(a), ',',hasDuplicates(b));
<script src="http://cdnjs.cloudflare.com/ajax/libs/lodash.js/3.1.0/lodash.min.js"></script>
回答by nackjicholson
You could check that there is _.someelement in the array which does not return its own location when looked up in the array. In other words, there is at least one element which has a match earlier in the array.
您可以检查_.some数组中是否存在在数组中查找时不返回其自身位置的元素。换句话说,至少有一个元素在数组中较早匹配。
function hasDuplicates(array) {
return _.some(array, function(elt, index) {
return array.indexOf(elt) !== index;
});
}
Perhaps this is faster than the _.uniqsolution, since it will identify the first duplicated element right away without having to compute the entire unique-ified array.
也许这比_.uniq解决方案更快,因为它会立即识别第一个重复元素,而无需计算整个唯一化数组。
Or, depending on your coding style and desire for readability, and if you want to use ES6 arrow functions for brevity:
或者,根据您的编码风格和对可读性的渴望,以及如果您想使用 ES6 箭头函数来简洁:
var earlierMatch = (elt, index, array) => array.indexOf(elt) !== index;
var hasDuplicates = array => _.some(array, earlierMatch);
回答by Akrion
As of ES6 you can simply use Setso this becomes:
从 ES6 开始,您可以简单地使用Set使其变为:
let hasDuplicates = arr => new Set(arr).size != arr.length
console.log(hasDuplicates([5,3,2,1,2,1,2,1]))
console.log(hasDuplicates([1,2,3,4,5]))
Which somewhat negates the use of lodash in this particular case.
在这种特殊情况下,这在某种程度上否定了 lodash 的使用。
回答by Paul Wostenberg
Well, there's always. lodash's _.uniq()function. That function actually returns a new array that only contains unique values, so checking to see if the length of the array has changed would get you your 'true' or 'false' value to return yourself, I believe.
嗯,总是有的。lodash 的_.uniq()函数。该函数实际上返回一个仅包含唯一值的新数组,因此检查数组的长度是否已更改将使您返回自己的“真”或“假”值,我相信。
回答by sebnukem
I don't know lodash but I submit:
我不知道 lodash,但我提交:
_.any(_.countBy(['foo', 'foo', 'bar']), function(x){ return x > 1; });
The problem with all the solutions proposed so far is that the entire input array needs processing to get an answer, even if the answer is obvious from the first 2 elements of the array.
到目前为止提出的所有解决方案的问题是整个输入数组需要处理才能得到答案,即使答案从数组的前 2 个元素中是显而易见的。
回答by Hushen Savani
No need to use lodash, use following code instead:
无需使用lodash,而是使用以下代码:
function getDuplicates(array, key) {
return array.filter(e1=>{
if(array.filter(e2=>{
return e1[key] === e2[key];
}).length > 1) {
return e1;
}
})
}

