Andrius Mozuraitis
Andrius Mozuraitis

Reputation: 93

How to divide array array with specific pattern

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:

enter image description here

This is real life example what i trying to do:enter image description here

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

enter image description here

Upvotes: 3

Views: 79

Answers (1)

Sampath Liyanage
Sampath Liyanage

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

Related Questions