Created
March 29, 2019 04:03
-
-
Save alenthomas/a197a48b2b932b0cc87c6934d6fcd01f to your computer and use it in GitHub Desktop.
1d-array to 2d-array with reduce
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
let postObject = posts.reverse().reduce((acc, ele) => { | |
if(acc.s > 0) { | |
return {arr: [ele, ...acc.arr], rows: acc.rows, s: acc.s-1} | |
} else { | |
return {arr: [ele], rows: [acc.arr, ...acc.rows], s: 2}; | |
} | |
}, {arr: [], rows: [], s: 3}); | |
let rows = [[...postObject.arr], ...postObject.rows ]; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment