Skip to content

Instantly share code, notes, and snippets.

@fenixkim
Last active December 11, 2015 11:58
Show Gist options
  • Save fenixkim/4597212 to your computer and use it in GitHub Desktop.
Save fenixkim/4597212 to your computer and use it in GitHub Desktop.
Divide an array object in equals parts
/**
Divide array in equal parts
@param array:Array reference array
@param parts:uint Number of Parts to divide
@returns Divided Array thus [[1,2,3],[4,5,6],[7,8,9,10]]
*/
public static function divideArray (array:Array, parts:uint):Array
{
if (parts <= 1)
throw new ArgumentError("Parts arguments must be integer greater than 1");
var groupSize:uint = Math.round(array.length / parts);
var result:Array = [];
for (var i:uint = 0; i < parts; i++) {
var group:Array;
if (i == parts-1)
group = array.slice(groupSize*i, array.length);
else
group = array.slice(groupSize*i, (groupSize * (i+1)));
result.push(group);
}
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment