Skip to content

Instantly share code, notes, and snippets.

@DefectingCat
Last active April 22, 2022 09:00
Show Gist options
  • Save DefectingCat/d519d448263e7d8180e971a42a631d2d to your computer and use it in GitHub Desktop.
Save DefectingCat/d519d448263e7d8180e971a42a631d2d to your computer and use it in GitHub Desktop.
一种不完善的数组去重方法
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();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment