Last active: 3 years ago
一种不完善的数组去重方法
Array.prototype.unique = function () {
let x,
y;
let uni = {};
for (x of this) {
uni[x] = 'xfy';
}
let arr = Object.keys(uni);
return arr;
}
Array.prototype.unique = function () {
let temp = {},
arr = [];
let len = this.length;
for (let i = 0; i < len; i++) {
if(!temp[this[i]]) {
temp[this[i]] = 'xfy';
arr.push(this[i]);
}
}
return arr;
}
// 不完善
Array.prototype.unique = function () {
let arr = [],
x,
y;
for (x of this) {
if (y === x) continue;
arr.push(x);
y = x;
}
return arr;
}
let arr = [1, 2, 2, 2, 3, 3, 4, 5, 6, 6, 7, 8, 9, 9, 'a', 'a', 'a', 'b', 'b', 'b', 'c'];
arr.unique();