Skip to content

Instantly share code, notes, and snippets.

@ralphsaunders
Last active December 21, 2015 08:29
Show Gist options
  • Save ralphsaunders/6278593 to your computer and use it in GitHub Desktop.
Save ralphsaunders/6278593 to your computer and use it in GitHub Desktop.
core.chunk = function (array, chunkSize) {
/// <summary>
/// Splits a given array into separate arrays of the given chunkSize
/// </summary>
/// <param name="array" type="array"></param>
/// <param name="chunkSize" type="number"></param>
/// <returns type="array"></returns>
var count = 1;
var newArray = [];
var limit = chunkSize;
var tempArray = [];
for (var i = 0; i < array.length ; i++) {
if (i < limit) {
tempArray.push(array[i]);
if (i == array.length - 1) {
newArray.push(tempArray);
tempArray = [];
}
}
else {
newArray.push(tempArray);
tempArray = [];
tempArray.push(array[i]);
count = count + 1;
limit = chunkSize * (count);
}
}
return newArray;
}
def chunk(list, n):
# Split list into chunks of n size
return [list[i:i+n] for i in range(0, len(list), n)]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment