divide arrangement into 3 arrangements

1

I have an arrangement that I want to divide into 3 arrangements, from 4 to 4.

for example

``````    [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22
,23,24,25,26,27,28,29,30,31,32,33,34,35,36]
``````

the result I want

``````[1,2,3,4,13,14,15,16,25,26,27,28]
[5,6,7,8,17,18,19,20,29,30,31,32]
[9,10,11,12,21,22,23,24,33,34,35,36]
``````

This I did

``````var a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36];
dividir(a);

function dividir(array) {
let array1 = [];
let array2 = [];
let array3 = [];
array.forEach(function(e, i) {
if (i % 10 < 4) {
array1.push(e);
}
if (i % 10 >= 4 && i % 10 < 8) {
array2.push(e);
}
if (i % 10 > 7 && i % 10 < 12) {
array3.push(e);
}
})
console.log('TAMS', array1.length, array2.length, array3.length);
return [array1, array2, array3];
}``````

asked by hubman 23.07.2018 в 06:09
source

2

What you are looking for is to divide the array into three parts as they are divided by twelve:

``````var a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36];
dividir(a);

function dividir(array) {
let array1 = [];
let array2 = [];
let array3 = [];
array.forEach(function(e, i) {
if (i % 12 < 4) {
array1.push(e);
} else if (/*i % 10 >= 4 &&*/ i % 12 < 8) {
array2.push(e);
} else if (/*i % 10 > 7 && */i % 12 < 12) {
array3.push(e);
}
})
console.log('TAMS', array1.length, array2.length, array3.length);
return [array1, array2, array3];
}``````