Skip to content

Instantly share code, notes, and snippets.

@scarletquasar
Last active December 14, 2021 18:58
Show Gist options
  • Save scarletquasar/217734a86916d1cc20825d77defd7f40 to your computer and use it in GitHub Desktop.
Save scarletquasar/217734a86916d1cc20825d77defd7f40 to your computer and use it in GitHub Desktop.
Chunk array with dynamic group length
let res = [];
let base = [1, 2, 3, 4];
let groupQuantity = 2;
for(let i = 0; i < base.length; i += groupQuantity) {
let even = [];
for(let gq = 0; gq < groupQuantity; gq++) {
base[i + gq] ? even.push(base[i + gq]) : {};
}
res.push(even);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment