lodash:从数组中获取重复值 [英] lodash: Get duplicate values from an array

查看:30
本文介绍了lodash:从数组中获取重复值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个这样的数组:[1, 1, 2, 2, 3]

Say I have an array like this: [1, 1, 2, 2, 3]

我想获得在这种情况下的重复项:[1, 2]

I want to get the duplicates which are in this case: [1, 2]

lodash 支持吗?我想以最短的方式完成.

Does lodash support this? I want to do it in the shortest way possible.

推荐答案

你可以使用这个:

_.filter(arr, (val, i, iteratee) => _.includes(iteratee, val, i + 1));

请注意,如果一个数字在数组中出现两次以上,您始终可以使用 _.uniq.

Note that if a number appears more than two times in your array you can always use _.uniq.

这篇关于lodash:从数组中获取重复值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆