How to get unique values in an array

后端 未结 20 2347
情歌与酒
情歌与酒 2020-11-22 14:02

How can I get a list of unique values in an array? Do I always have to use a second array or is there something similar to java\'s hashmap in JavaScript?

I am going

20条回答
  •  暗喜
    暗喜 (楼主)
    2020-11-22 14:53

    Here's a much cleaner solution for ES6 that I see isn't included here. It uses the Set and the spread operator: ...

    var a = [1, 1, 2];
    
    [... new Set(a)]
    

    Which returns [1, 2]

提交回复
热议问题