How to create a list of unique items in JavaScript?

后端 未结 8 424
半阙折子戏
半阙折子戏 2020-12-09 11:54

In my CouchDB reduce function I need to reduce a list of items to the unique ones.

Note: In that case it\'s ok to have a list, it will be a small number of items

8条回答
  •  無奈伤痛
    2020-12-09 12:26

    The best method seem to be using ES6 and Set. Single line and faster* than above according to fiddle

        
    const myList = [1,4,5,1,2,4,5,6,7];
    const unique = [...new Set(myList)];
        
    console.log(unique);

    *tested in safari

提交回复
热议问题