Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert a JavaScript Typed Array into a JavaScript Array

Tags:

javascript

I have a JavaScript Float32Array, and I would like to convert it into a regular JavaScript Array. How can I do this?

like image 720
stuax Avatar asked Oct 06 '12 14:10

stuax


People also ask

What is a typed array JavaScript?

JavaScript typed arrays are array-like objects that provide a mechanism for reading and writing raw binary data in memory buffers. Array objects grow and shrink dynamically and can have any JavaScript value. JavaScript engines perform optimizations so that these arrays are fast.

Why might you use typed arrays instead of standard arrays?

A typed array significantly simplifies the level of proof the engine needs to be able to optimise around it. A value returned from a typed array is certainly of a certain type, and engines can optimise for the result being that type.

How can you create an array in JavaScript using array literal?

Creating an Array Using an array literal is the easiest way to create a JavaScript Array. Syntax: const array_name = [item1, item2, ...]; It is a common practice to declare arrays with the const keyword.


1 Answers

If you don't need to support old browsers (including IE, unfortunately), you can use Array.from, which was added to ES6:

var array = Array.from(floatarr); 

This now works in the new releases of every browser (except IE), and it works on all major mobile browsers too.

like image 127
Cody Piersall Avatar answered Sep 22 '22 22:09

Cody Piersall