Javascript array_chunk : Arrays : PHP functions in JavaScript JAVASCRIPT TUTORIALS


JAVASCRIPT TUTORIALS » PHP functions in JavaScript » Arrays »

 

Javascript array_chunk


Split array into chunks
Example 1

Running
1.array_chunk(['Kevin', 'van', 'Zonneveld'], 2);

Could return
1.{0 : {0: 'Kevin', 1: 'van'} , 1 : {0: 'Zonneveld'}}
Dependencies

No dependencies, you can use this function standalone.
function array_chunk( input, size ) {
    // Split array into chunks  
    // 
    // version: 810.114
    // discuss at: http://phpjs.org/functions/array_chunk
    // +   original by: Carlos R. L. Rodrigues (http://www.jsfromhell.com)
    // *     example 1: array_chunk(['Kevin', 'van', 'Zonneveld'], 2);
    // *     returns 1: {0 : {0: 'Kevin', 1: 'van'} , 1 : {0: 'Zonneveld'}}
 
    for(var x, i = 0, c = -1, l = input.length, n = []; i < l; i++){
        (x = i % size) ? n[c][x] = input[i] : n[++c] = [input[i]];
    }

    return n;
}


HTML code for linking to this page:

Follow Navioo On Twitter

JAVASCRIPT TUTORIALS

 Navioo PHP functions in JavaScript
» Arrays