tags:

views:

35

answers:

1

I have a chunk of data coming from an ajax call. I need to split this chunk up into smaller chunks of a predefined amount, not by a delimiter character, but by an amount. 2k is what I would like. I am trying to come up with a snippet that would bit shift divide the data, and place each chunk including the remaining data into arrays. So if the original chunk is 1 meg, then I would have chunk[0]=2k of data, chunk[1]=next 2k of data..... and so on until there is no more data to assign.

Thanks Pat

+1  A: 

I think bit shifting is only working with numbers..

Assuming you are dealing with string data, you could just loop and extract the data..

var data = 'abcdefghijklmnopqrstuvwxyz'; // you actual data from the ajax call
var chunks = [];

var chunksize = 10; // how many chars to hold per chunk, 2048 would be your case

for (var i = 0; i < data.length / chunksize; i++)
{
  chunks.push( data.substring(i*chunksize, (i+1)*chunksize) );
}

data = ''; // just to free the memory ..

Example at http://jsfiddle.net/MYWNN/1/

Gaby
@Gaby: thanks, this is exactly what I needed. Seems so simple now that I am looking at your code.
cube