lodash:从数组中获得重复的值 [英] lodash: Get duplicate values from an array
本文介绍了lodash:从数组中获得重复的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
说我有一个这样的数组: [1,1,2,2,3]
Say I have an array like this: [1, 1, 2, 2, 3]
和我想要得到的重复: [1,2]
And I want to get the duplicates: [1, 2]
是否lodash支持呢?我想这样做在最短的路。
Does lodash support this? I want to do it in the shortest way possible.
推荐答案
您可以使用此:
_.filter(array, function (value, index, iteratee) {
return _.includes(iteratee, value, index + 1);
});
请注意,如果出现一些阵列中两个以上的时候,你可以随时使用 _。uniq的
。
Note that if a number appears more than two times in your array you can always use _.uniq
.
这篇关于lodash:从数组中获得重复的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文