Get all unique values in an array (remove duplicates)

I have an array of numbers that I need to make sure are unique. I found the code snippet below on the internet and it works great until the array has a zero in it. I found this other script here on SO that looks almost exactly like it, but it doesn't fail.

So for the sake of helping me learn, can someone help me determine where the prototype script is going wrong?

Array.prototype.getUnique = function() {
 var o = {}, a = [], i, e;
 for (i = 0; e = this[i]; i++) {o[e] = 1};
 for (e in o) {a.push (e)};
 return a;
}

More answers from duplicate question:

  • Remove Duplicates from JavaScript Array
  • Similar question:

  • Get all values with more than one occurrence (ie: not unique) in an array

  • With JavaScript 1.6 / ECMAScript 5 you can use the native filter method of an Array in the following way to get an array with unique values:

    function onlyUnique(value, index, self) { 
        return self.indexOf(value) === index;
    }
    
    // usage example:
    var a = ['a', 1, 'a', 2, '1'];
    var unique = a.filter( onlyUnique ); // returns ['a', 1, 2, '1']
    

    The native method filter will loop through the array and leave only those entries that pass the given callback function onlyUnique .

    onlyUnique checks, if the given value is the first occurring. If not, it must be a duplicate and will not be copied.

    This solution works without any extra library like jQuery or prototype.js.

    It works for arrays with mixed value types too.

    For old Browsers (<ie9), that do not support the native methods filter and indexOf you can find work arounds in the MDN documentation for filter and indexOf.

    If you want to keep the last occurrence of a value, simple replace indexOf by lastIndexOf .

    With ES6 it could be shorten to this:

    // usage example:
    var myArray = ['a', 1, 'a', 2, '1'];
    var unique = myArray.filter((v, i, a) => a.indexOf(v) === i); 
    
    // unique is ['a', 1, 2, '1']
    

    Thanks to Camilo Martin for hint in comment.

    ES6 has a native object Set to store unique values. To get an array with unique values you could do now this:

    var myArray = ['a', 1, 'a', 2, '1'];
    
    let unique = [...new Set(myArray)]; 
    
    // unique is ['a', 1, 2, '1']
    

    The constructor of Set takes an iterable object, like Array, and the spread operator ... transform the set back into an Array. Thanks to Lukas Liese for hint in comment.


    Updated answer for ES6/ES2015 : Using the Set, the single line solution is:

    var items = [4,5,4,6,3,4,5,2,23,1,4,4,4]
    var uniqueItems = Array.from(new Set(items))
    

    Which returns

    [4, 5, 6, 3, 2, 23, 1]
    

    As le_m suggested, this can also be shortened using spread operator , like

    var uniqueItems = [...new Set(items)]
    

    你也可以使用underscore.js。

    console.log(_.uniq([1, 2, 1, 3, 1, 4]));
    <script src="http://underscorejs.org/underscore-min.js"></script>
    链接地址: http://www.djcxy.com/p/19036.html

    上一篇: 将数组元素从一个数组位置移到另一个数组位置

    下一篇: 获取数组中的所有唯一值(删除重复项)