Reputation: 93
I need divide array: Into N ( N is number of new arrays ) arrays with certain patern.
Array is dynamic and values can change.
This is illustrated example what i want to do:
This is real life example what i trying to do:
I need divide one array in this pattern and create N arrays: In examples i created 2 arrays but now imagine that i want create 99 arrays
Upvotes: 3
Views: 79
Reputation: 4896
Something like this will work..
var array = ['a','b','c','d','e'];
var results = devideArray(array,2);
function devideArray(array, count){
var result = [];
for (var i=0; i<count; i++){
result[i] = [];
}
for (var i=0; i<array.length; i++){
result[i%count].push(array[i]);
}
return result;
}
Upvotes: 1