Get all non-unique values (i.e.: duplicate/more than one occurrence) in an array

后端 未结 30 3166
再見小時候
再見小時候 2020-11-21 04:35

I need to check a JavaScript array to see if there are any duplicate values. What\'s the easiest way to do this? I just need to find what the duplicated values are - I don\'

30条回答
  •  别那么骄傲
    2020-11-21 05:28

    var a = [324,3,32,5,52,2100,1,20,2,3,3,2,2,2,1,1,1].sort();
    a.filter(function(v,i,o){return i&&v!==o[i-1]?v:0;});
    

    or when added to the prototyp.chain of Array

    //copy and paste: without error handling
    Array.prototype.unique = 
       function(){return this.sort().filter(function(v,i,o){return i&&v!==o[i-1]?v:0;});}
    

    See here: https://gist.github.com/1305056

提交回复
热议问题