Skip to content

Instantly share code, notes, and snippets.

@BirkhoffLee
Created September 18, 2017 13:38
Show Gist options
  • Save BirkhoffLee/3da8bf144893c9e93a7ac2869c8a67f3 to your computer and use it in GitHub Desktop.
Save BirkhoffLee/3da8bf144893c9e93a7ac2869c8a67f3 to your computer and use it in GitHub Desktop.
let a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
const unflatten = (arrayToUnflatten, unflattenLength) => {
return Promise.all(new Array(Math.ceil(arrayToUnflatten.length / unflattenLength)).fill('').map((_, index) => new Promise(resolve => {
resolve(arrayToUnflatten.slice(index * unflattenLength, index * unflattenLength + unflattenLength))
})))
}
unflatten(a, 2).then(result => console.log(result))
@BirkhoffLee
Copy link
Author

birkhoff at Birkhoff-MBPR in ~/Desktop
$ node Untitled-1.js
[ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ], [ 10 ] ]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment