3

I've a Float32Array of 10659503 elements in length. I want to chunk (split) it into small arrays of fixed length. How to do that? I've tried the method bellow it's not working:

var LENGTH = 4096;

var chunking = function(data) {
    var chunks = [];
    for (var i = 0; i < data.length; i += LENGTH) {
        var index = ~~(i/LENGTH);
        var offset = i%LENGTH;
        if (offset === 0) {
            chunks[i] = data.subarray(i, LENGTH);
        }
    }
    console.log(chunks);
}

Thank you,

ket
  • 945
  • 1
  • 9
  • 15
  • 1
    possible duplicate of [Split array into chunks](http://stackoverflow.com/questions/8495687/split-array-into-chunks) – Felix Kling Mar 23 '14 at 06:58
  • And [many more](http://stackoverflow.com/search?q=%5Bjavascript%5D+array+chunk) – Felix Kling Mar 23 '14 at 07:00
  • I've tried several methods you suggested. It isn't solved my problem. The code above I've collected from those threads. – ket Mar 23 '14 at 07:07
  • Keep in mind that typed arrays don't inherit from Array.prototype, so instead of e.g. arr.slice(i), you have to write Array.protoype.slice.call(arr, i). Or use subarray like you already do. The overall approach is the same though. – Felix Kling Mar 23 '14 at 07:11

3 Answers3

4

You can use Float32Array.subarray() get the sliced view of the original array.

function chunking(data, length) {
    var result = [];
    for (var i = 0; i < data.length; i += length) {
        result.push(data.subarray(i, i + length));
    }
    return result;
}

console.log(chunking(new Float32Array(100), 12));
thefourtheye
  • 233,700
  • 52
  • 457
  • 497
0

In general case I would do it using splice

function chunk(data, LENGTH) {
  var ret = [] 
  while (data.length) {
   ret.push(data.splice(0,LENGTH))
  }
  return ret
}

But since Float32Array doesn't have this method in prototype (thanks @Felix Kling), it is going to be little bit more complicated:

function chunk(data, LENGTH) {
  var ret = [] 
  while (data.length) {
   ret.push([].splice.call(data, 0, LENGTH))
  }
  return ret
}

However, since you probably want to avoid coping data back and forth in memory, you have to use methods that just create new ArrayBufferView without moving actual data. And that is exactly what subarray method is doing. So @thefourtheye answer is the right way to go.

vittore
  • 17,449
  • 6
  • 44
  • 82
-1

Try this:

newArray = (function(array, chunk) {
    retArr = [];
    for (var i = 0; i < array.length; i+=chunk)
       retArr.push(array.slice(i,i+chunk));
    return retArr;
}(oldArray, chunkSize))

http://jsfiddle.net/R3Ume/7/

Mosho
  • 7,099
  • 3
  • 34
  • 51