How to create a list of unique items in JavaScript?

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

    I find the other answers to be rather complicated for no gain that I can see.

    We can use the indexOf method of the Array to verify if an item exists in it before pushing:

    const duplicated_values = ['one', 'one', 'one', 'one', 'two', 'three', 'three', 'four'];
    const unique_list = [];
    
    duplicated_values.forEach(value => {
      if (unique_list.indexOf(value) === -1) {
        unique_list.push(value);
      }
    });
    
    console.log(unique_list);

    That will work with any type of variable as well, even objects (given the identifier actually reference the same entity, merely equivalent objects are not seen as the same).

    0 讨论(0)
  • 2020-12-09 12:13

    This is an old question, I know. However, it is at the top of some google searches, so I wanted to add that you can combine the answers from @RobHague and @EugeneNaydenov using the following:

    function unique(arr) {
      const u = {};
      return arr.filter((v) => {
        return u[v] = !u.hasOwnProperty(v);
      });
    };
    

    You can also ignore undefined values (often handy) by adding:

    function unique(arr) {
      const u = {};
      return arr.filter((v) => {
        return u[v] = (v !== undefined && !u.hasOwnProperty(v));
      });
    };
    

    You can play with this solution here: https://jsfiddle.net/s8d14v5n/

    0 讨论(0)
  • 2020-12-09 12:16

    This should work with anything, not just strings:

    export const getUniqueList =  (a: Array<any>) : Array<any> => {
    
      const set = new Set<any>();
    
      for(let v of a){
          set.add(v);
      }
    
      return Array.from(set);
    
    };
    

    the above can just be reduced to:

    export const getUniqueValues = (a: Array<any>) => {
       return Array.from(new Set(a));
    };
    

    :)

    0 讨论(0)
  • 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), []);
    }
    
    0 讨论(0)
  • 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

    0 讨论(0)
  • 2020-12-09 12:28

    what about

        function unique(list) {
          for (i = 0; i<list.length; i++) {
            for (j=i+1; j<list.length; j++) {
              if (list[i] == list[j]) {
                list.splice(j, 1);
              }
            }
          }
        }
    
    0 讨论(0)
提交回复
热议问题