-1

I have an array that can be of any length. and need to split it into sections. The first section will be a length of 14, and there after a length of 16

var size1 = 14;
var size2 = 16;
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, 37, 38, 39, 40];
var arrays = [];
if (a.length > 14){
    for (i = 0; i < 14; i++) {
        arrays.push(a.splice(0, size1));
    } 
    for (i = 14 ; i < a.length; i++){
        arrays.push(a.splice(0, size2));

    }
} else {
    arrays.push(a.splice(0, size1));

}
console.log(arrays);

However based on what I am doing my array keeps splitting only at 14. Can you advice on how I can do this?

Thank you

Cleared
  • 2,490
  • 18
  • 35
edcoder
  • 503
  • 1
  • 5
  • 19

4 Answers4

1

The solution using Array.prototype.slice() function:

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, 37, 38, 39, 40],
    size1 = 14,
    size2 = 16,
    arrays = [];

[0, size1, size2].forEach(function (v, i, arr) {
    arrays.push((arr[i+1])? a.slice(v, v + arr[i+1]) : a.slice(arr[i-1] + v));
});

console.log(arrays);
RomanPerekhrest
  • 88,541
  • 4
  • 65
  • 105
  • Hi @Roman, thank you for your help. But my array stops at 2 (i.e. 14, 16).. the rest of the numbers need to go into the next array section. Can you advice how I can automate that? – edcoder May 04 '17 at 10:54
  • you mean: the first two chunks of fixed length and the last chunk should contain all remaining items? – RomanPerekhrest May 04 '17 at 11:03
  • so it should be 14, 16, 16, 16, 16, 16 ...etc... the first section will be length of 14, and then on it should be length of 16. – edcoder May 04 '17 at 11:07
  • unclear, can you show how should look the entire final array? – RomanPerekhrest May 04 '17 at 11:08
  • pardon me for not being clear, so in my case array a has numbers upto 40. so it should be array[0] = 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14 ; array[1] = 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30 ; array[2] = 31, 32, 33, 34, 35, 36, 37, 38, 39, 40 . If there are more numbers it will keep adding in sections of 16 . Currently, since we are doing arrays.push(a.slice(0, size1) and arrays.push(size1, size1 + size2) it is stopping at arrays[2] – edcoder May 04 '17 at 11:09
1

You could use an array for the chunk length and a zero for the rest and map the chunks by keeping the length of the previous lengths.

It works for an arbitrary count of chunks.

var chunks = [14, 16, 0],
    array = [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, 37, 38, 39, 40],
    result = chunks.map((last => a => array.slice(last, a ? (last += a) : undefined))(0));

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
0

Edit: so, like this then?

var size1 = 14;
var size2 = 16;
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, 37, 38, 39, 40];

var head = a.slice(0, size1);
var arrays = [head];
while (size1 < a.length){
    arrays.push(a.slice(size1, Math.min(size1+size2, a.length)));
    size1 += size2;
}

console.log(arrays)
Jonas
  • 116
  • 5
0

You can try this with all dynamic array sizes, it will get you desired output. Hope you were looking for this solution.

var size1 = 14;
var size2 = 16;
var flag=0;
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, 37, 38, 39, 40];
var arrays = [];
var t=a.length/14;
while(a.length>0){
        if (flag==0 && a.length > 14){
                arrays.push(a.splice(0, size1));
                flag=1;
        } else if(a.length>=16){
            arrays.push(a.splice(0, size2));
        }
        else{
            arrays.push(a.splice(0, a.length));
        }
}
        console.log(arrays)
anurag0510
  • 763
  • 1
  • 8
  • 17