1

I am trying to break an larger array into smaller chunks.

Here is an example:

   var data = [ { number: '184805871' },
  { number: '3418400730' },
  { number: '2047901973' },
  { number: '5038880529' },
  { number: '5040293507' },
  { number: '5044243187' },
  { number: '5078304520' },
  { number: '5085466763' },
  { number: '5145133307' },
  { number: '5197540781' },
  { number: '5199065039' },
  { number: '5208120208' },
  { number: '5212123861' },
  { number: '5212383329' },
  { number: '5214353364' },
  { number: '5229836037' }
  ];

My array is much longer than this, this is just an example. So I am familar with the array_chunk function in PHP and found it in php.js

function array_chunk (input, size, preserve_keys) {

    var x, p = '', i = 0, c = -1, l = input.length || 0, n = [];

    if (size < 1) {
        return null;
    }

    if (Object.prototype.toString.call(input) === '[object Array]') {
        if (preserve_keys) {
            while (i < l) {
                (x = i % size) ? n[c][i] = input[i] : n[++c] = {}, n[c][i] = input[i];
                i++;
            }
        }
        else {
            while (i < l) {
                (x = i % size) ? n[c][x] = input[i] : n[++c] = [input[i]];
                i++;
            }
        }
    }
    else {
        if (preserve_keys) {
            for (p in input) {
                if (input.hasOwnProperty(p)) {
                    (x = i % size) ? n[c][p] = input[p] : n[++c] = {}, n[c][p] = input[p];
                    i++;
                }
            }
        }
        else {
            for (p in input) {
                if (input.hasOwnProperty(p)) {
                    (x = i % size) ? n[c][x] = input[p] : n[++c] = [input[p]];
                    i++;
                }
            }
        }
    }
    return n;
}

Is there an easier way to do this than that?

Here is my code:

var chunks = array_chunk(data, 3);

jQuery.each(chunks, function(index, value) { 
  console.log(index + ': ' + value); 
});

How can I get the value from number to console.log instead of [Object]? I tried value[0] etc but it isn't working..

For each array that is split into 3 per array, I need to get the index and the value from the array.

How can I do this?

Pointy
  • 405,095
  • 59
  • 585
  • 614
jordan
  • 11
  • 1
  • 2
  • The problem is chunks is 3 arrays, so you still need another foreach inside the chunks foreach. http://jsfiddle.net/a4ntM/ – Mark Sep 26 '12 at 06:19
  • Possible duplicate of [Split array into chunks](http://stackoverflow.com/questions/8495687/split-array-into-chunks) – Mogsdad Dec 07 '15 at 15:37

2 Answers2

1

As your question is unclear to me, are you aware you can easily slice any array in javascript ?

to "take the data array and split it into separate array with only 3 numbers per array" :

var chunks = [];
for (var i=0; i<data.length;) {
    chunks.push(data.slice(i, i+=3));
}

to "go thru each array and get the values of each along with the index for the array" :

for (var i=0; i<chunks.length; i++) { // iter among chunks
   for (var j=0; j<chunks[i].length; j++) {  // iter in chunk
       console.log(j, chunks[i][j].number); // show item j of the chunk number i
   }
}

EDIT :

if you want to display

...
12 ["5212123861", "5212383329", "5214353364"] 
13 ["5212123861", "5212383329", "5214353364"] 
14 ["5212123861", "5212383329", "5214353364"] 
15 ["5229836037"] 

you may iterate like this :

for (var i=0; i<chunks.length; i++) { 
   for (var j=0; j<chunks[i].length; j++) {  
       console.log(i*3+j, chunks[i].map(function(v){return v.number})); 
   }
}
Denys Séguret
  • 372,613
  • 87
  • 782
  • 758
  • I'll try to explain better -- I want to take the data array and split it into separate array with only 3 numbers per array. I then want to go thru each array and get the values of each along with the index for the array. – jordan Sep 26 '12 at 06:16
  • Thanks for the response, I tried your code that goes thru each array to get the values, however the output is 0 number, 1 number, 0 number, etc -- Say it split it into 50 different pieces, I want it to display 0 - 1,2,3,4,5,6, etc/ 1 - 1,2,3,4,5,6 / with all the numbers for each piece. Do you understand or do I need to clarify better? – jordan Sep 26 '12 at 06:29
  • 1
    Do you want one of those : [fiddle A](http://jsfiddle.net/dystroy/NFKMj/) [fiddle B](http://jsfiddle.net/dystroy/UPceB/) [fiddle C](http://jsfiddle.net/dystroy/PKDXH/) ? – Denys Séguret Sep 26 '12 at 06:39
0

The array.slice method can extract a slice from the beginning, middle, or end of an array for whatever purposes you require.

var i,j,temparray,chunk = 10;
for (i=0,j=data.length; i<j; i+=chunk) {
    temparray = data.slice(i,i+chunk);
    // do whatever
}
Bill
  • 25,119
  • 8
  • 94
  • 125