Using lodash to check whether an array has duplicate values

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

    As of ES6 you can simply use Set so this becomes:

    let hasDuplicates = arr => new Set(arr).size != arr.length
    
    console.log(hasDuplicates([5,3,2,1,2,1,2,1]))
    console.log(hasDuplicates([1,2,3,4,5]))

    Which somewhat negates the use of lodash in this particular case.

提交回复
热议问题