lodash: Get duplicate values from an array

时间:2015-07-28 16:09:50

标签: javascript arrays lodash

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 this? I want to do it in the shortest way possible.

13 个答案:

答案 0 :(得分:33)

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.

答案 1 :(得分:19)

另一种方法是按唯一项分组,并返回包含多个项

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

答案 2 :(得分:13)

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.

答案 3 :(得分:5)

另一种方法,但使用过滤器和ecmaScript 2015(ES6)

var array = [1, 1, 2, 2, 3];

_.filter(array, v => 
  _.filter(array, v1 => v1 === v).length > 1);

//→ [1, 1, 2, 2]

答案 4 :(得分:4)

如何使用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]

http://jsbin.com/panama/edit?js,console

答案 5 :(得分:1)

那么你可以使用这段代码,因为它具有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]

答案 6 :(得分:1)

这是我的,es6-like,deps-free,回答。用过滤器代替减速器

// 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

编辑: 嗯,我的坏处是:我读过q作为一般性问题,但这是严格意义上的lodash,不过我的观点是 - 你不需要lodash在这里:)

答案 7 :(得分:1)

纯js解决方案

export function hasDuplicates(array) {
  return new Set(array).size !== array.length
}

答案 8 :(得分:0)

希望以下解决方案对您有帮助,它将在所有情况下均有用

  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);

答案 9 :(得分:0)

这是另一个简洁的解决方案:

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负责_.filter附带的配音。

与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))

答案 10 :(得分:0)

无需使用lodash,您可以使用以下代码:

function getDuplicates(array, key) {
  return array.filter(e1=>{
    if(array.filter(e2=>{
      return e1[key] === e2[key];
    }).length > 1) {
      return e1;
    }
  })
}

答案 11 :(得分:0)

您可以使用counter对象。这会将每个数字作为键,并将出现的总数作为其值。当数字的计数器变为2时,您可以使用filter来获取数字

const array = [1, 1, 2, 2, 3],
      counter = {};
      
const duplicates = array.filter(n => (counter[n] = counter[n] + 1 || 1) === 2)

console.log(duplicates)

答案 12 :(得分:0)

为什么不使用这个?

_.uniq([4, 1, 5, 1, 2, 4, 2, 3, 4]) // [4, 1, 5, 2, 3]