Created
August 19, 2015 19:49
-
-
Save djm/7072b3adf10c68de5a9a to your computer and use it in GitHub Desktop.
Javascript: chunk an ES6 Map into an Array of similar sized Maps
This file contains hidden or 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
| export function chunkMap (map, chunkSize) { | |
| const chunkedMaps = [] | |
| const mapAsArray = Array.from(map) | |
| for (var i = 0; i < map.size; i += chunkSize) { | |
| let chunked = mapAsArray.slice(i, i + chunkSize) | |
| chunkedMaps.push(new Map(chunked)) | |
| } | |
| return chunkedMaps | |
| } |
Author
function chunk(arr, size){
let chunked = [];
for(let ele of arr){
let last = chunked[chunked.length - 1]
if(!last || last.length === size){
chunked.push([ele])
}else{
last.push(ele)
}
}
return chunked
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
If you come across this and have found a more efficient way, then please feel free to leave it in the comments.