0

How to create a chunks of array by part base on given parts count..

for example ..

var myarray = [1,2,3,4,5,6,7,8,9,10];
var bypart = 3 ;

makechunks(bypart);

expected output is ..

[1,2,3],[4,5,6],[7,8,9,10]

from large array to 3 small arrays

Jakegarbo
  • 1,201
  • 10
  • 20

3 Answers3

1

There is no built in way in either javascript or angular.

I assume you want all parts to be roughly the same size.

function splitArray(array, groups) {
    var sets = [];
    var groupsize = array.length / groups;

    for (var i = 0, j = 0; i < groups; i++, j += groupsize) {
      sets[i] = array.slice(j, j + groupsize);
    }

    return sets;
}

For your given input splitArray([1,2,3,4,5,6,7,8,9,10], 3) the result will be [[1,2,3],[4,5,6],[7,8,9,10]]

Coloured Panda
  • 3,293
  • 3
  • 20
  • 30
1

You could use only a fraction of the length and add it to the index.

function chunk(array, count) {
    var result = [],
        size = array.length / count;
        i = 0;

    while (i < array.length) {
        result.push(array.slice(i, i += size));
    }

    return result;
}

console.log(chunk([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 3));
console.log(chunk([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11], 3));
.as-console-wrapper { max-height: 100% !important; top: 0; }
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
-1

Did you try to google?

Here is what you need:

var createGroupedArray = function(arr, chunkSize) {
    var groups = [], i;
    for (i = 0; i < arr.length; i += chunkSize) {
        groups.push(arr.slice(i, i + chunkSize));
    }
    return groups;
}

Usage example:

var arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14];
var groupedArr = createGroupedArray(arr, 4);
var result = JSON.stringify(groupedArr);
// result: "[[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14]]"

Taken from: http://www.frontcoded.com/splitting-javascript-array-into-chunks.html

UPDATED:

var makechunks = function(arr, chunksAmount) {
    var chunkSize = Math.ceil(arr.length/chunksAmount);
    var groups = [], i;
    for (i = 0; i < arr.length; i += chunkSize) {
        groups.push(arr.slice(i, i + chunkSize));
    }
    return groups;
}
Sergej
  • 2,030
  • 1
  • 18
  • 28
  • Links are not preferred as they tend to die. Add the contents in your answer. – Coloured Panda Aug 03 '17 at 12:07
  • i already try it .. so close but .. not the correct function as i expected to use. – Jakegarbo Aug 03 '17 at 12:11
  • I've added an updated version, where you put chunksAmount, but not a chunkSize. May be that is what you need? – Sergej Aug 03 '17 at 12:19
  • it makes the element limit by 4 . so that the result make more than the given size .. like this sample as i am testing chunks([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 3)); the goal is from large array to 3 small arrays – Jakegarbo Aug 03 '17 at 12:31
  • I've added an updated example, where the 2nd parameter takes the amount of chunks, instaed of the chunk size. – Sergej Aug 03 '17 at 12:44
  • Thanks! :) it also work now. useful line var chunkSize = Math.ceil(arr.length/chunksAmount); – Jakegarbo Aug 03 '17 at 13:11