1

Suppose we have an array of variable length, and I want to process it by chunks that are of a maximum length of 100, and do it in the minimum number of chunks. So for an array of length 241, it would be 3 sub arrays of sizes 41, 100, 100 (or 100, 100, 41).

curr_len = arr.length;
offset = curr_len%100;
doSomethingWithSubArray(arr.slice(offset))

for(j = offset; j <= curr_len; j = j+100){
    doSomethingWithSubArray(arr.slice(j,j+100))
}

I'm sure there are more elegant ways of doing this, possibly without the special case before the for loop. Any ideas?

5 Answers 5

2

I'd expect the last chunk to be of smaller size. The code then would be:

for (var i=0; i<arr.length; i+=100)
    doSomethingWithSubArray(arr.slice(i, 100));

This is exactly what my splitBy function does:

Array.prototype.splitBy = function(n) {
/* get: number of items per array
return: array of n-sized arrays with the items (last array may contain less then n) */
    for (var r=[], i=0; i<this.length; i+=n)
        r.push(this.slice(i, i+n));
    return r;
}

Then write only:

arr.splitBy(100).forEach(doSomethingWithSubArray);
Sign up to request clarification or add additional context in comments.

2 Comments

Of course. For some reason, my brain was avoiding this to avoid an IndexOutOfRange. I guess javascript is wonderful.
my solution is consice better yours how about this?
1

use chunk function~

function chunk(a, s){
    for(var x, i = 0, c = -1, l = a.length, n = []; i < l; i++)
        (x = i % s) ? n[c][x] = a[i] : n[++c] = [a[i]];
    return n;
}

console.log(chunk([1,2,3,4,5,6,7,8,9,10], 3));

Comments

1

it's functional style recursive solutions. no var, no loop, no count, because it's more cleary

var chunk = function(arr, n){
    if (arr.length == 0) return [];
    var head = arr.slice(0, n), rest = arr.slice(n);

    return [head].concat( chunk(rest, n) );
};

console.log(chunk([1,2,3,4,5,6,7,8,9,10], 3));​

1 Comment

No "var" is not really true :-) However, most people understand loops easier than recursive calls - and they're faster, too.
0

Not really, using reduce looks like this:

var array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11];

var splitArrays = array.reduce(function(arr, cur, i) {
    if (i % 3 === 0) arr.push([]);
    arr[i / 3 | 0].push(cur);
    return arr;
}, []);
//splitArrays looks like:
//[[1,2,3],[4,5,6],[7,8,9],[10,11]]

More generic function

function splitArray(array, num) {
    return array.reduce(function(arr, cur, i) {
        if (i % num === 0) arr.push([]);
        arr[i / num | 0].push(cur);
        return arr;
    }, []);
}

Comments

0

Make your doSomethingWithSubArray function accept a starting index and return a next unprocessed index or null if there's no more work. Put this "iterator" in a while loop. Do rest of work that you want to do between chunks (update UI?) right after calling this "iterator" in a while condition.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.