Skip to content

Instantly share code, notes, and snippets.

@tai2
Created September 26, 2018 01:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tai2/347cb43d1d36da0e7b775f8347987581 to your computer and use it in GitHub Desktop.
Save tai2/347cb43d1d36da0e7b775f8347987581 to your computer and use it in GitHub Desktop.
[1,2,3,4,5] to [[1,2], [3,4], [5,null]]
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