Javascript lodash:从数组中获取重复值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31681732/
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: Get duplicate values from an array
提问by zianwar
回答by saadel
You can use this:
你可以使用这个:
_.filter(arr, (val, i, iteratee) => _.includes(iteratee, val, i + 1));
Note that if a number appears more than two times in your array you can always use _.uniq
.
请注意,如果一个数字在数组中出现两次以上,您始终可以使用_.uniq
.
回答by gafi
Another way is to group by unique items, and return the group keys that have more than 1 item
另一种方法是按唯一项分组,并返回超过 1 个项的组键
_([1, 1, 2, 2, 3]).groupBy().pickBy(x => x.length > 1).keys().value()
回答by Mikhail Romanov
var array = [1, 1, 2, 2, 3];
var groupped = _.groupBy(array, function (n) {return n});
var result = _.uniq(_.flatten(_.filter(groupped, function (n) {return n.length > 1})));
This works for unsorted arrays as well.
这也适用于未排序的数组。
回答by Rafael Zeffa
Another way, but using filters and ecmaScript 2015 (ES6)
另一种方式,但使用过滤器和 ecmaScript 2015 (ES6)
var array = [1, 1, 2, 2, 3];
_.filter(array, v =>
_.filter(array, v1 => v1 === v).length > 1);
//→ [1, 1, 2, 2]
回答by Brian Park
How about using countBy()
followed by reduce()
?
使用countBy()
后跟怎么样reduce()
?
const items = [1,1,2,3,3,3,4,5,6,7,7];
const dup = _(items)
.countBy()
.reduce((acc, val, key) => val > 1 ? acc.concat(key) : acc, [])
.map(_.toNumber)
console.log(dup);
// [1, 3, 7]
回答by Rahul Surabhi
Well you can use this piece of code which is much faster as it has a complexity of O(n) and this doesn't use Lodash.
好吧,您可以使用这段代码,它的速度要快得多,因为它的复杂度为 O(n),并且不使用 Lodash。
[1, 1, 2, 2, 3]
.reduce((agg,col) => {
agg.filter[col] = agg.filter[col]? agg.dup.push(col): 2;
return agg
},
{filter:{},dup:[]})
.dup;
//result:[1,2]
回答by vonsko
here is mine, es6-like, deps-free, answer. with filter instead of reducer
这是我的,类似 es6,无 deps 的答案。用过滤器代替减速器
// this checks if elements of one list contains elements of second list
// example code
[0,1,2,3,8,9].filter(item => [3,4,5,6,7].indexOf(item) > -1)
// function
const contains = (listA, listB) => listA.filter(item => listB.indexOf(item) > -1)
contains([0,1,2,3], [1,2,3,4]) // => [1, 2, 3]
// only for bool
const hasDuplicates = (listA, listB) => !!contains(listA, listB).length
edit: hmm my bad is: I've read q as general question but this is strictly for lodash, however my point is - you don't need lodash in here :)
编辑:嗯,我的坏处是:我已将 q 作为一般问题阅读,但这仅适用于 lodash,但我的观点是 - 您不需要 lodash 在这里:)
回答by Akrion
Here is another concise solution:
这是另一个简洁的解决方案:
let data = [1, 1, 2, 2, 3]
let result = _.uniq(_.filter(data, (v, i, a) => a.indexOf(v) !== i))
console.log(result)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.min.js"></script>
_.uniq
takes care of the dubs which _.filter
comes back with.
_.uniq
照顾_.filter
回来的配音。
Same with ES6 and Set:
与 ES6 和Set相同:
let data = [1, 1, 2, 2, 3]
let result = new Set(data.filter((v, i, a) => a.indexOf(v) !== i))
console.log(Array.from(result))
回答by Hushen Savani
No need to use lodash
, you can use following code:
无需使用lodash
,您可以使用以下代码:
function getDuplicates(array, key) {
return array.filter(e1=>{
if(array.filter(e2=>{
return e1[key] === e2[key];
}).length > 1) {
return e1;
}
})
}
回答by Vijay Kesanupalli
Hope below solution helps you and it will be useful in all conditions
希望以下解决方案对您有所帮助,并且在所有情况下都会有用
hasDataExist(listObj, key, value): boolean {
return _.find(listObj, function(o) { return _.get(o, key) == value }) != undefined;
}
let duplcateIndex = this.service.hasDataExist(this.list, 'xyz', value);