Skip to content

Instantly share code, notes, and snippets.

@bitfishxyz
Created January 19, 2020 01:23
Show Gist options
  • Save bitfishxyz/5eae08011c8eeb757221bfa377c1c196 to your computer and use it in GitHub Desktop.
Save bitfishxyz/5eae08011c8eeb757221bfa377c1c196 to your computer and use it in GitHub Desktop.
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