Last active
May 11, 2019 06:32
-
-
Save gaoryrt/c07ae01b18ee2d64d56422a07bbe241c to your computer and use it in GitHub Desktop.
flatten a nested array
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 isArr = arg => Object.prototype.toString.call(arg) === '[object Array]' | |
const flat = inputAny => | |
( | |
isArr(inputAny[0]) | |
? flat(inputAny[0]) | |
: [inputAny[0]] | |
) | |
.concat( | |
inputAny.length > 1 | |
? flat(inputAny.slice(1)) | |
: [] | |
) | |
console.log(flat([0, [1, [[2, 3]], 4]])) |
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 Y = f => (x => f(x(x)))(x => f((...y) => x(x)(...y))) | |
const isArr = arg => Object.prototype.toString.call(arg) === '[object Array]' | |
const flat = Y( | |
f => inputAny => | |
( | |
isArr(inputAny[0]) | |
? f(inputAny[0]) | |
: [inputAny[0]] | |
) | |
.concat( | |
inputAny.length > 1 | |
? f(inputAny.slice(1)) | |
: [] | |
) | |
) | |
console.log(flat([0, [1, [[2, 3]], 4]])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment