Created
January 19, 2020 01:23
-
-
Save bitfishxyz/5eae08011c8eeb757221bfa377c1c196 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
const selfFlat = function (depth = 1) { | |
let arr = Array.prototype.slice.call(this) | |
if (depth === 0) return arr | |
return arr.reduce((pre, cur) => { | |
if (Array.isArray(cur)) { | |
return [...pre, ...selfFlat.call(cur, depth - 1)] | |
} else { | |
return [...pre, cur] | |
} | |
}, []) | |
} | |
Array.prototype.selfFlat = selfFlat; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment