javascript

How to flatten a Float32Array in JS?


I have an array filled with Float32Arrays like this:

var arr = [
  new Float32Array([0.3, 0.7, 0.1]),
  new Float32Array([0.2, 0.544, 0.21]),
];

console.log(arr.flat(Infinity));

How do I flatten them to be like this?:

[0.3, 0.7, 0.1, 0.2, 0.544, 0.21]

I have tried use Array.flat(Infinity) but this still leaves the Float32Arrays intact.


Solution

  • For example

    let arr = [
      new Float32Array([0.3, 0.7, 0.1]),
      new Float32Array([0.2, 0.544, 0.21]),
    ];
    
    arr = arr.map(a => [...a]).flat();
    
    console.log(arr);