lodash: Get duplicate values from an array

前端 未结 12 2213
执念已碎
执念已碎 2020-12-09 08:14

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

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

Does lodash support thi

12条回答
  •  离开以前
    2020-12-09 08:25

    Another way is to group by unique items, and return the group keys that have more than 1 item

    _([1, 1, 2, 2, 3]).groupBy().pickBy(x => x.length > 1).keys().value()
    

提交回复
热议问题