Last active
April 22, 2022 09:00
-
-
Save DefectingCat/d519d448263e7d8180e971a42a631d2d to your computer and use it in GitHub Desktop.
一种不完善的数组去重方法
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 不完善 | |
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