Created
June 4, 2022 06:34
-
-
Save ichengzi/b7c99ed9f5fa563610884d4d392b2a8d 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
// https://www.bilibili.com/video/BV1BL411K7Dy | |
const arr = [ | |
[1, 2, 2], | |
[3, 4, 5, 5], | |
[6, 7, 8, 9, | |
[11, 12, | |
[12, 12, | |
[13] | |
] | |
] | |
], | |
10 | |
]; | |
Array.prototype.myFlat = function () { | |
const result = []; | |
this.forEach(item => { | |
if (Array.isArray(item)) { | |
item.myFlat().forEach(x => result.push(x)); | |
} else { | |
result.push(item); | |
} | |
}) | |
return result; | |
} | |
Array.prototype.myUnique = function() { | |
return Array.from(new Set(this)); | |
} | |
const compareFn = (a,b) => a-b; | |
console.log(arr.myFlat()); | |
console.log(arr.myFlat().myUnique()); | |
console.log(arr.myFlat().myUnique().sort(compareFn)); | |
// 如果myFlat() 传入一个 Set,那么 flat, unique 可以同时实现 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment