封装数组去重的方法
一、
function noRepeat(arr) {
var arr1 = [];
for (var i = 0; i < arr.length; i++) {
var item = arr[i];
var flag = true;
for (var j = 0; j < arr1.length; j++) {
if (arr1[j] === item) {
flag = false;
break;
}
}
if (flag) arr1.push(item);
}
return arr1;
}
console.log(noRepeat(arr));
二、
function noRepeat(arr) {
for (var i = 0; i < arr.length; i++) {
var item = arr[i];
for (var j = i + 1; j < arr.length; j++) {
if (item === arr[j]) {
delete arr[j];
}
}
}
var arr1 = [];
for (var prop in arr) {
arr1.push(arr[prop]);
}
return arr1;
}
console.log(noRepeat(arr));
三、
function noRepeat(arr) {
var arr1 = [];
for (var i = 0; i < arr.length; i++) {
var item = arr[i];
if (arr1.indexOf(item) === -1) arr1.push(item);
}
return arr1;
}
console.log(noRepeat(arr));