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:
parent
feb47caa6b
commit
3a7042e527
1 changed files with 24 additions and 0 deletions
|
@ -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
|
||||
*
|
||||
|
|
Loading…
Reference in a new issue