jordan
jordan

Reputation: 11

Breaking array into chunks

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?

Upvotes: 1

Views: 2691

Answers (3)

Oleg Simindey
Oleg Simindey

Reputation: 1

function array_chunk(array, chunkSize) {
    if (!array.length || chunkSize < 1) return [];

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

    return chunks;
}

var data = [1, 2, 3, 4, 5, 6, 7, 8, 9];
console.log(array_chunk(data, 3)); // [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

Upvotes: 0

Denys S&#233;guret
Denys S&#233;guret

Reputation: 382160

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})); 
   }
}

Upvotes: 1

Bill
Bill

Reputation: 25555

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
}

Upvotes: 0

Related Questions