How to create a list of unique items in JavaScript?

后端 未结 8 411
半阙折子戏
半阙折子戏 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:24

    Using Object.keys will give you strings if you put in integer arguments (uniq([1,2,3]) => ['1','2','3']. Here's one with Array.reduce:

    function uniq(list) {
        return list.reduce((acc, d) => acc.includes(d) ? acc : acc.concat(d), []);
    }
    

提交回复
热议问题