Using lodash to check whether an array has duplicate values

前端 未结 6 687
独厮守ぢ
独厮守ぢ 2020-12-30 21:57

What do you all think would be the best (best can be interpreted as most readable or most performant, your choice) way to write a function using the lodash utilities in orde

6条回答
  •  星月不相逢
    2020-12-30 22:39

    I don't know lodash but I submit:

    _.any(_.countBy(['foo', 'foo', 'bar']), function(x){ return x > 1; });
    

    The problem with all the solutions proposed so far is that the entire input array needs processing to get an answer, even if the answer is obvious from the first 2 elements of the array.

提交回复
热议问题