Skip to content

Instantly share code, notes, and snippets.

@djm
Created August 19, 2015 19:49
Show Gist options
  • Select an option

  • Save djm/7072b3adf10c68de5a9a to your computer and use it in GitHub Desktop.

Select an option

Save djm/7072b3adf10c68de5a9a to your computer and use it in GitHub Desktop.
Javascript: chunk an ES6 Map into an Array of similar sized Maps
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
}
@djm
Copy link
Author

djm commented Aug 19, 2015

If you come across this and have found a more efficient way, then please feel free to leave it in the comments.

@ls-miles-rostami
Copy link

ls-miles-rostami commented Apr 30, 2018

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