Using lodash to check whether an array has duplicate values

前端 未结 6 688
独厮守ぢ
独厮守ぢ 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:35

    You can try this code:

    function hasDuplicates(a) {
      return _.uniq(a).length !== a.length; 
    }
    
    var a = [1,2,1,3,4,5];
    var b = [1,2,3,4,5,6];
    
    document.write(hasDuplicates(a), ',',hasDuplicates(b));

提交回复
热议问题