javascript原型:写一个合并后数组去掉同类项的方法

<!DOCTYPE html>
<html>
<head>
    <title>test013_Array_prototype_unique()</title>    
</head>
<script type="text/javascript">
    Array.prototype.unique = function() {
        var a = {};
            for (var i = 0; i < this.length; i++) {
                if (typeof a[this[i]] == "undefined")
                a[this[i]] = 1; 
            }
        this.length = 0;
            for(var i in a)
            this[this.length] = i;
            return this;            
    };    
</script>
<script type="text/javascript">
    var a = [1,2,3];
    var b = [2,3,4];
    var c = a.concat(b).unique();
    console.log(c);
</script>
<body>            
</body>
</html>

例子中unique()方法是可以提成单独的文件的。

结果:

原文地址:https://www.cnblogs.com/foxcharon/p/7156844.html