0

Lets say i have this

    var array =[23,345,6,765,54423,45654,7657,43,43,5,765,3456,768,897,545,645,87,4556,432,543,534];
var chunkEachBy = [3,2,1,5,6,4];

var chunked = [];


for(var i =0, l = chunkEachBy.length; i < l; i++)
{
     chunked.push(array.splice(0, chunkEachBy[i]));   
}

output:

[Array[3], Array[2], Array[1], Array[5], Array[6], Array[4]]

which simply chunks array into the length of the chunkEachBy, then chunk each value for each chunkEachBy.

My question is, instead of pushing the result of this into a seperate array in this case named chunked can this all be done to the original array and produce the same result?

JSFiddle - https://jsfiddle.net/b7twnurm/1/

11
  • 1
    array = chunked at the end of the code? You can't logically write an array in a loop if you need to access it in read mode to create the new one. Commented Apr 13, 2015 at 12:50
  • 1
    Could you provide an example of result? Commented Apr 13, 2015 at 12:52
  • 1
    @BenConnorHansell yes you could definitely do it (in a loop, contrary to what Jack wrote), but it's not clear why you're interested in doing that. Commented Apr 13, 2015 at 12:52
  • 1
    @Pointy: so you'd write the original array while you're reading it in a for cycle to create the new data? What'd you use as index, if you're shrinking it? Commented Apr 13, 2015 at 12:54
  • 1
    Well you're going to end up building the "chunks" as new arrays anyway, no matter where you put them. Commented Apr 13, 2015 at 12:57

2 Answers 2

10

Use a double splice with i as index:

array.splice(i, 0, array.splice(i, chunkEachBy[i]));

Explanation:

i points to the first element of the array that we have not yet processed, or after the last element we added.
The splice taking two arguments (array.splice(i, chunkEachBy[i])) removes a number of elements from array from position i, and the one taking three arguments inserts them all back to the position they were just taken from, but now as an array (because that's what array.splice returns).

Also see the array.splice documentation for the third parameter.

Updated fiddle: https://jsfiddle.net/b7twnurm/2/

Sign up to request clarification or add additional context in comments.

2 Comments

Also, i didn't realize that splice could take a third parameter!
Thank you very much for the explanation! Now, i'm going to test the speed of doing this against declaring and pushing the result into an array - i'll write back to see which is better! However i will still use this, as it's much tidier than what i have!
1

Try the follow:

var array = 23,345,6,765,54423,45654,7657,43,43,5,765,3456,768,897,545,645,87,4556,432,543,534];
var chunkEachBy = [3,2,1,5,6,4];
var chunked = [];

for(var i =0, l = chunkEachBy.length; i < l; i++)
{
     array.unshift(array.splice(i, chunkEachBy[i]));   
}

1 Comment

This works too, however it does miss off the final value of the last chunked array, so a +1 would be needed - chunkEachBy[i]+1

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.