Zotero.Utilities.forEachChunk(arr, chunkSize, func)

Run a function on chunks of a given size of an array's elements and
return an array with the return values from the successive runs.
This commit is contained in:
Dan Stillman 2013-03-28 19:22:14 -04:00
parent feb47caa6b
commit 3a7042e527

View file

@ -624,6 +624,30 @@ Zotero.Utilities = {
return a;
},
/**
* Run a function on chunks of a given size of an array's elements.
*
* @param {Array} arr
* @param {Integer} chunkSize
* @param {Function} func
* @return {Array} The return values from the successive runs
*/
"forEachChunk":function(arr, chunkSize, func) {
var retValues = [];
var tmpArray = arr.concat();
var num = arr.length;
var done = 0;
do {
var chunk = tmpArray.splice(0, chunkSize);
done += chunk.length;
retValues.push(func(chunk));
}
while (done < num);
return retValues;
},
/**
* Generate a random integer between min and max inclusive
*