Skip to content

Instantly share code, notes, and snippets.

@inlife
Created February 24, 2020 07:25
Show Gist options
  • Save inlife/f61f70e717ac6a5172d074d039d3d689 to your computer and use it in GitHub Desktop.
Save inlife/f61f70e717ac6a5172d074d039d3d689 to your computer and use it in GitHub Desktop.
const array2d = array1d
.reduce((cur, i) => {
/* transform 1d array to 2d */
let lastInd = cur.length-1
let lastVal = cur[lastInd]
if (lastVal.length === 3) {
return cur.concat([[i]])
}
return cur
.slice(0, lastInd)
.concat([lastVal.concat(i)])
}, [[]])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment