Search code examples
javascriptarraysfloating-pointarraybuffer

How to turn array of arraybuffers into either a float32array or a normal array in javascript?


I have an array of arraybuffers, meaning that I have

array[1] -> once this is clicked on, in the console, it holds this 

[object ArrayBuffer][object ArrayBuffer][object ArrayBuffer][object ArrayBuffer][object ArrayBuffer][object ArrayBuffer][object ArrayBuffer][object ArrayBuffer][object ArrayBuffer][object ArrayBuffer][object ArrayBuffer][object ArrayBuffer][object ArrayBuffer]

I have to take these arraybuffers and turn them back into arrays that I can read from. Originally, I had only one of these and was using

    temp_float_array = new Float32Array(arr_buff); //turns array buffer data from hex bytes to an array of floats
    for (var i = 0; i < temp_float_array.length; i++) {
        float_array[i] = parseFloat(temp_float_array[i]);
    }

to take the arraybuffer and turn it into a float32array, and then from there using parseFloat to make it a normal array. Now that I have a lot of these arraybuffers I need to turn each one to a human readable array. Thanks for any help or advice.

Each arraybuffer holds about 50K values in it,

[-91.51844787597656,-91.5179443359375,-91.5174560546875,-91.51744842529297,-91.55493927001953,-91.5665283203125,-91.57928466796875,-91.59294128417969,-91.607177734375,-91.62168884277344...etc]

ideally I need these values back just as they are.


Solution

  • For anybody in the future this is what I ended up doing.

        temp_float_array = new Float32Array(arr_buff); //turns array buffer data from hex bytes to an array of floats
                for (var j = 0; j < temp_float_array.length; j++) {
                    part_arr[j] = parseFloat(temp_float_array[j]);
                }
                float_array = float_array + part_arr;
    

    I used a nested for loop. This was inside of my main for loop, arr_buff was each individual arraybuffer. This gave me an array of float32 arrays for each arraybuffer. Outside of my main for loop I then had

    float_array = float_array.split(',');
    for (var u = 0; u < float_array.length; u++) {
        float_array[u] = parseFloat(float_array[u]);
    }
    

    Splitting the values at the commas and then taking float_array, which now has my array of float32 arrays and then turned that into one massive array with a few million values. Not the cleanest or fastest solution I'm sure, but my working solution for now.