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?

2
  • The problem is chunks is 3 arrays, so you still need another foreach inside the chunks foreach. jsfiddle.net/a4ntM Commented Sep 26, 2012 at 6:19
  • Possible duplicate of Split array into chunks Commented Dec 7, 2015 at 15:37

3 Answers 3

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})); 
   }
}
Sign up to request clarification or add additional context in comments.

3 Comments

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.
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?
Do you want one of those : fiddle A fiddle B fiddle C ?
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
}

Comments

0
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]]

1 Comment

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.