Use PHP functions in JavaScript

JavaScript array_chunk

Split array into chunks

1
2
3
4
56
7
8
9
1011
12
13
14
1516
17
18
19
2021
22
23
24
2526
27
28
29
3031
32
33
34
3536
37
38
39
4041
42
43
44
4546
47
48
49
5051
52
53
54
5556
57
function array_chunk (input, size, preserve_keys) {
    // Split array into chunks  
    // 
    // version: 1109.2015
    // discuss at: http://phpjs.org/functions/array_chunk    // +   original by: Carlos R. L. Rodrigues (http://www.jsfromhell.com)
    // +   improved by: Brett Zamir (http://brett-zamir.me)
    // %        note 1: Important note: Per the ECMAScript specification, objects may not always iterate in a predictable order
    // *     example 1: array_chunk(['Kevin', 'van', 'Zonneveld'], 2);
    // *     returns 1: [['Kevin', 'van'], ['Zonneveld']]    // *     example 2: array_chunk(['Kevin', 'van', 'Zonneveld'], 2, true);
    // *     returns 2: [{0:'Kevin', 1:'van'}, {2: 'Zonneveld'}]
    // *     example 3: array_chunk({1:'Kevin', 2:'van', 3:'Zonneveld'}, 2);
    // *     returns 3: [['Kevin', 'van'], ['Zonneveld']]
    // *     example 4: array_chunk({1:'Kevin', 2:'van', 3:'Zonneveld'}, 2, true);    // *     returns 4: [{1: 'Kevin', 2: 'van'}, {3: 'Zonneveld'}]
    
    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;
}
external links: original PHP docs | raw js source

Examples

» Example 1

Running

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

Should return

1
[['Kevin', 'van'], ['Zonneveld']]

» Example 2

Running

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

Should return

1
[{0:'Kevin', 1:'van'}, {2: 'Zonneveld'}]

Dependencies

No dependencies, you can use this function standalone.

Open syntax issues

php.js uses JsLint to help us keep our code consistent and prevent some common bugs.

Eventually we want all code to pass or at least take into consideration most fixes suggested by JsLint, following this JsLint configuration we’ve decided on.


Authors

Thanks to the following developers, you get to have array_chunk goodness in JavaScript.

Comments

Add Comment
Use:
[CODE]
your_stuff('here');
[/CODE]
for proper code formatting
By submitting code here you are allowing us to use it in php.js hence dual licensing it under the MIT and GPL licenses

Gravatar
Douwe
13 Jan '09 Permalink

q  You use constant_name as a function parameter, but in the function you use constant...

Gravatar
Kevin van Zonneveld
13 Apr '08 Permalink

q  @ Jonas Raoni: Done!

Gravatar
Jonas Raoni
12 Apr '08 Permalink

q  Put a link on the name of Carlos R. L. Rodrigues to our site at http://jsfromhell.com ^^


Contribute a New function