JavaScript | typedArray.from() with Examples

The typedArray.from() is an inbuilt function in JavaScript which is used to construct a new typedArray from a normal array or any iterable object.
List of different typedArrays are specified below-

  • Int8Array();
  • Uint8Array();
  • Uint8ClampedArray();
  • Int16Array();
  • Uint16Array();
  • Int32Array();
  • Uint32Array();
  • Float32Array();
  • Float64Array();

Syntax:

typedArray.from(source, mapFn, thisArg)

Parameters: It accepts three parameters which are specified below-



  • source: It is a normal array or any iterable object for converting to a typedArray.
  • mapFn: It is optional and it is map function to call on every element of the typedArray.
  • thisArg: It is optional and it is the value to be used while executing mapFn function.

Return value: It returns a new typedArray instance.
JavaScript code to show the working of this function:
Code #1:

filter_none

edit
close

play_arrow

link
brightness_4
code

<script>
  
    // Constructing an iterable object
    var a = new Set([ 5, 10, 15, 20, 25 ]);
    var b = new Set([ 1, 2, 3, 4, 5 ]);
    var c = new Set([ 1, 3, 5, 7, 9 ]);
    var d = new Set([ 2, 4, 6, 8, 10 ]);
  
    // Calling from() function
    A = Uint8Array.from(a);
    B = Uint8Array.from(b);
    C = Uint8Array.from(c);
    D = Uint8Array.from(d);
  
   // Printing new typedArray instance
    document.write(A + "<br>");
    document.write(B + "<br>");
    document.write(C + "<br>");
    document.write(D);
  
</script>

chevron_right


Output:

5, 10, 15, 20, 25
1, 2, 3, 4, 5
1, 3, 5, 7, 9
2, 4, 6, 8, 10

Code #2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<script>
  
    // Calling from() function
    A = Uint16Array.from('123456');
    B = Uint16Array.from('80397418327');
  
    // Printing new typedArray instance
    document.write(A + "<br>");
    document.write(B + "<br>");
  
</script>

chevron_right


Output:

1, 2, 3, 4, 5, 6
8, 0, 3, 9, 7, 4, 1, 8, 3, 2, 7

Code #3:

filter_none

edit
close

play_arrow

link
brightness_4
code

<script>
  
    // Constructing an iterable object
    var a = new Set([ 5, 10, 15, 20, 25 ]);
    var b = new Set([ 1, 2, 3, 4, 5 ]);
    var c = new Set([ 1, 3, 5, 7, 9 ]);
    var d = new Set([ 2, 4, 6, 8, 10 ]);
  
    // Calling from() function
    A = Uint8Array.from(a, x => x + 1);
    B = Uint8Array.from(b, x => x + 2);
    C = Uint8Array.from(c, x => x * 2);  
    D = Uint8Array.from(d);
  
    // Printing new typedArray instance
   document.write(A + "<br>");
   document.write(B + "<br>");
   document.write(C + "<br>");
   document.write(D);
  
</script>

chevron_right


Output:

6, 11, 16, 21, 26
3, 4, 5, 6, 7
2, 6, 10, 14, 18
2, 4, 6, 8, 10


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.