js如何判断一个数组里有多少个是相同的

2025-05-08 10:22:35
推荐回答(1个)
回答1:

var a = [5,4,3,2,1,2,3,2,1,];  
Array.prototype.duplicate=function() {  
    var tmp = [];  
    this.concat()().sort().sort(function(a,b){  
        if(a==b && tmp.indexOf(a) === -1) tmp.push(a);  
    });  
    return tmp.length;  
}  
  
document.write(a.duplicate())

return 里写tmp可以返回重复的元素是哪个    有.length是返回重复了多少个