Array.prototype.indexOf = Array.prototype.indexOf || function (searchElement, fromIndex) {
var index = -1;
fromIndex = fromIndex * 1 || 0;
for (var k = 0, length = this.length; k < length; k++) {
if (k >= fromIndex && this[k] === searchElement) {
index = k;
break;
}
}
return index;
};
Array.prototype.filter = Array.prototype.filter || function (fn, context) {
var arr = [];
if (typeof fn === "function") {
for (var k = 0, length = this.length; k < length; k++) {
fn.call(context, this[k], k, this) && arr.push(this[k]);
}
}
return arr;
};
// 数组去重
Array.prototype.unique = function() {
var n = {}, r = [];
for (var i = 0; i < this.length; i++) {
if (!n[this[i]]) {
n[this[i]] = true;
r.push(this[i]);
}
}
return r;
}
Array.intersect = function(arr1, arr2) {
if(Object.prototype.toString.call(arr1) === "[object Array]" && Object.prototype.toString.call(arr2) === "[object Array]") {
return arr1.filter(function(v){
return arr2.indexOf(v)!==-1
})
}
}
// 使用方式
Array.intersect([1,2,3,4], [3,4,5,6]); // [3,4]
Array.union = function(arr1, arr2) {
if(Object.prototype.toString.call(arr1) === "[object Array]" && Object.prototype.toString.call(arr2) === "[object Array]") {
return arr1.concat(arr2).unique()
}
}
// 使用方式
Array.union([1,2,3,4], [1,3,4,5,6]); // [1,2,3,4,5,6]
Array.prototype.minus = function(arr) {
if(Object.prototype.toString.call(arr) === "[object Array]") {
var interArr = Array.intersect(this, arr);// 交集数组
return this.filter(function(v){
return interArr.indexOf(v) === -1
})
}
}
// 使用方式
var arr = [1,2,3,4];
arr.minus([2,4]); // [1,3]
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有