zoukankan      html  css  js  c++  java
  • javascript数组去重

    直接上代码:

    Array.prototype.distinct = function () { 
        var newArr = [],obj = {}; 
        for(var i=0, len = this.length; i < len; i++){ 
            if(!obj[typeof(this[i]) + this[i]]){ 
                newArr.push(this[i]); 
                obj[typeof(this[i]) + this[i]] = 'new'; 
            } 
        } 
        return newArr; 
    } 

    加强版:

    Array.prototype.distinct = function () { 
        var sameObj = function(a, b){ 
            var tag = true; 
            if(!a || !b) return false; 
            for(var x in a){ 
                if(!b[x]) return false; 
                if(typeof(a[x]) === 'object'){ 
                    tag = sameObj(a[x],b[x]); 
                } else { 
                    if(a[x]!==b[x]) 
                    return false; 
                } 
            } 
            return tag; 
        } 
        var newArr = [], obj = {}; 
        for(var i = 0, len = this.length; i < len; i++){ 
            if(!sameObj(obj[typeof(this[i]) + this[i]], this[i])){ 
            newArr.push(this[i]); 
            obj[typeof(this[i]) + this[i]] = this[i]; 
            } 
        } 
        return newArr; 
    } 
  • 相关阅读:
    常用网址记录
    css一些兼容问题
    css hack
    js 闭包
    js 继承
    js 实现淘宝放大镜
    css做三角形的方法
    js 轮播效果
    css3特效
    css布局
  • 原文地址:https://www.cnblogs.com/fengyuqing/p/array_unique.html
Copyright © 2011-2022 走看看