JS数组去重

去重方法很多,提供比较好用方法

方法一:通过对象的属性是唯一的特点,判断是否重复

Array.prototype.distinct = function(){
    var arr = this;
    var i,obj={},result=[],l=arr.length;
    for (var i=0;i<l;i++){
        if(!obj[arr[i]]){
            obj[arr[i]] = true;
            result.push(arr[i]);
        }
    }
    return result;
}
var a = [0,0,1,1,1,13,8,18,9,9,9,9,5];
console.log(a.distinct());  //  [0, 1, 13, 8, 18, 9, 5]
原文地址:https://www.cnblogs.com/hcxwd/p/9350703.html