Skip to content

Instantly share code, notes, and snippets.

@rodpoblete
Last active February 25, 2023 18:24
Show Gist options
  • Save rodpoblete/2ff70a36781ba69b7d3e6c02f624fe22 to your computer and use it in GitHub Desktop.
Save rodpoblete/2ff70a36781ba69b7d3e6c02f624fe22 to your computer and use it in GitHub Desktop.
Dividir array en grupo de arrays - FCC [251]
// array.prototype.push()
// array.prototype.slice();
function chunckArrayInGroups(arr, size) {
var chunk = [], i; // declara array vacio e indice de for
for (i = 0; i <= arr.length; i+= size) // loop que recorre el array
chunk.push(arr.slice(i, i + size)); // push al array el tramo desde el indice del loop hasta el valor size + el indicador
return chunk;
}
chunckArrayInGroups(["a", "b", "c", "d"], 2);
// [["a", "b"], ["c","d"]]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment