JavaScript Array的原型扩展方法

>>>取两个数组的交集

///集合取交集
        Array.intersect = function () {
            var result = new Array();
            var obj = {};
            for (var i = 0; i < arguments.length; i++) {
                for (var j = 0; j < arguments[i].length; j++) {
                    var str = arguments[i][j];
                    if (!obj[str]) {
                        obj[str] = 1;
                    }
                    else {
                        obj[str]++;
                        if (obj[str] == arguments.length)
                        {
                            result.push(str);
                        }
                    }//end else
                }//end for j
            }//end for i
            return result;
        }

console.log(Array.intersect([“1”, “2”, “3”], [“2”, “3”, “4”, “5”, “6”]));//[2,3]

>>>去掉数组中的重复项

        Array.prototype.uniquelize = function () {
            var tmp = {},
                ret = [];
            for (var i = 0, j = this.length; i < j; i++) {
                if (!tmp[this[i]]) {
                    tmp[this[i]] = 1;
                    ret.push(this[i]);
                }
            }
 
            return ret;
        }

console.log([1, 2, 3, 2, 3, 4, 5, 6].uniquelize());//[1,2,3,4,5,6]

>>>并集数组

        Array.union = function () {
            var arr = new Array();
            var obj = {};
            for (var i = 0; i < arguments.length; i++) {
                for (var j = 0; j < arguments[i].length; j++)
                {
                    var str=arguments[i][j];
                    if (!obj[str])
                    {
                        obj[str] = 1;
                        arr.push(str);
                    }
                }//end for j
            }//end for i
            return arr;
        }

console.log(Array.union([“1”, “2”, “3”], [“2”, “3”, “4”, “5”, “6”], [“5”, “6”, “7”, “8”, “9”]))
//[“1”,“2”,“3”,“4”,“5”, “6”, “7”, “8”, “9”]

>>>差集:取数组A中除去数组B中的值

Array.prototype.minus = function (arr) {
            var result = new Array();
            var obj = {};
            for (var i = 0; i < arr.length; i++) {
                obj[arr[i]] = 1;
            }
            for (var j = 0; j < this.length; j++) {
                if (!obj[this[j]])
                {
                    obj[this[j]] = 1;
                    result.push(this[j]);
                }
            }
            return result;
        };

console.log([“2”, “3”, “4”, “5”, “6”].minus([“1”, “2”, “3”])); //[“4”,“5”,“6”]

>>>合并数组: A1+B1,A2+B2…

Array.prototype.merge = function (arr) {     
	var result = new Array();
	for (var i = 0; i < arr.length; i++) {
			result.push(this[i]+"_"+arr[i]);
	}
	return result;
	//return result.join("\n");    //合并数组为字符串
};

console.log([““a”, “b”, “c”, “d”].merge([“1”,“2”,“3”,“4”])); //[“a1”,“b2”,“c3”,“d4”]

>>>根据值删除数组项

Array.prototype.removeByValue = function(val) {
      for(var i = 0; i < this.length; i++) {
        if(this[i] == val) {
          this.splice(i, 1);
          break;
        }
      }
    }

var somearray = [“mon”, “tue”, “wed”, “thur”]
somearray.removeByValue(“wed”);

Reference:
https://blog.csdn.net/weixin_39717076/article/details/84837280

你可能感兴趣的:(JavaScript)