Array operation (de-duplication, deep copy …)

  array, Array de-duplication, javascript
  • Deep cloning

function clone(Obj) {
    var buf;
    if (Obj instanceof Array) {
        var buf = [];//创建一个空数组
        var i = Obj.length;
        while (i--) {
            buf[i] = clone(Obj[i]);
        }
        return buf;
    } else if (Obj instanceof Object) {
        buf = {};//创建一个空对象
        for (var k in Obj) {
            buf[k] = clone(Obj[k]);
        }
        return buf;
    } else {
        return Obj;
    }
}
  • Array De-duplication-Method One

Array.prototype.unique1 = function () {
    var tempArr = [];// 一个新的临时数组
    for (var i = 0; i < this.length; i++) {//遍历当前数组
        //如果当前数组的第i已经保存进了临时数组,那么跳过,
        //否则把当前项push到临时数组里面
        if (n.indexOf(this[i]) == -1) {
            n.push(this[i]);
        }
        return tempArr;
    }
};
  • Array De-duplication-Method 2

Array.prototype.unique2 = function () {
    var result = {},
        tempArr = [];//result为hash表,tempArr为临时数组
    for (var i = 0; i < this.length; i++) {//遍历当前数组
        if (!hash[this[i]]) {//如果hash表中没有当前项
            hash[this[i]] = true;//存入hash表
            tempArr.push(this[i]);//把当前数组的当前项push到临时数组里面
        }
    }
    return tempArr;
};
  • Array De-duplication-Method 3

Array.prototype.unique3 = function () {
    var result = [this[0]];//结果数组
    for (var i = 1; i < this.length; i++) {//从第二项开始遍历
        //如果当前数组的第i项在当前数组中第一次出现的位置不是i,
        //那么表示第i项是重复的,忽略掉。否则存入结果数组
        if (this.indexOf(this[i]) == i) {
            result.push(this[i]);
        }
    }
    return result;
};
  • 1. add a prototype method to the Array local object, which is used to delete duplicate entries (there may be more than one) in the array entries, and the return value is one containing [New array of deleted duplicate entries

Array.prototype.unique = function () {
    var result = [];
    for (var i = 0; i < this.length; i++) {
        for (var j = i + 1; j < this.length; j++) {
            if (this[i] === this[j]) {
                result.push(this.splice(j, 1)[0]);
            } else {
                j++;
            }
        }
    }
    return result;
};
console.log(['a', 'b', 'c', 'd', 'b', 'a', 'e', 'asd', 'as', 12, 56, 1, 12, 'as'].unique());
  • var a = [1,4,5,2,9]; The correct way to find the maximum value in A is as follows

var a = [1, 4, 5, 2, 9];
console.log(Math.max.apply(null, a));
  • Add a method to remove duplicates for Array object, and get [The de-duplicated array

Array.prototype.uniq = function () {
    var arr = [];
    var flag = true;
    for (var i = 0; i < this.length; i++) {
        if (arr.indexOf(this[i]) == -1) {
            if (this[i] != this[i]) {
                if (flag) {
                    arr.push(this[i]);
                    flag = false;
                }
            } else {
                arr.push(this[i])
            }
        }
    }
    return arr;
};
console.log([false, true, undefined, null, NaN, 0, 1, {}, {}, 'a', 'a', NaN].uniq());