How to split an array of an array with a specific pattern
I need to split an array: into N ( N number of new arrays ) arrays with a specific patern.
The array is dynamic and the values can change.
This is an illustrated example of what I want to do:
This is a real life example I'm trying to do:
I need to split one array in this pattern and create N arrays: in the examples I created 2 arrays, but now imagine that I want to create 99 arrays
+3
source to share
1 answer
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;
}
+1
source to share