var arr = [1, 2, 52, 41, 1, 11, 2, 25, 1, 4, 5, 2, 5, 2];
1.写在原型链中的数组去重:
Array.prototype.deleteRepeat = function () {
var obj = {},
arr = [],
len = this.length;
for(var i = 0; i < len; i ++) {
if(!obj[this[i]]) {
obj[this[i]] = "asd";
arr.push(this[i]);
}
}
return arr;
}
2.封装函数数组去重:(函数内len的arr随着数组名的变化而变化):
var Arr = function () {
var obj = {},
arrr = [],
len = arr.length;
for(var i = 0; i < len; i ++) {
if(!obj[arr[i]]) {
obj[arr[i]] = "asd";
arrr.push(arr[i]);
}
}
arr = arrr;
return arr;
}
js数组去重
最新推荐文章于 2024-06-01 07:27:22 发布