Skip to content

Instantly share code, notes, and snippets.

@alenthomas
Created March 29, 2019 04:03
Show Gist options
  • Save alenthomas/a197a48b2b932b0cc87c6934d6fcd01f to your computer and use it in GitHub Desktop.
Save alenthomas/a197a48b2b932b0cc87c6934d6fcd01f to your computer and use it in GitHub Desktop.
1d-array to 2d-array with reduce
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