-
-
Save tai2/347cb43d1d36da0e7b775f8347987581 to your computer and use it in GitHub Desktop.
[1,2,3,4,5] to [[1,2], [3,4], [5,null]]
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
const a = [1,2,3,4,5] | |
const b = a.reduce((acc, v, i, a) => { | |
const [left, right] = acc | |
if (i % 2 == 0) { | |
left.push(v) | |
if (i === a.length - 1) { | |
right.push(null) | |
} | |
} else { | |
right.push(v) | |
} | |
return acc | |
}, [[], []]) | |
console.log(b) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment