JavaScript | typedArray.map() with Examples

The typedArray.map() is an inbuilt function in JavaScript which is used to create a new typedArray with the result of a provided function on each element of the given typedArray.
Syntax:

typedArray.map(callback)

Parameters: It accepts a parameter callback function which accept some parameter which are specified below-

  • currentValue: It is the current element which is being processed in the typedArray.
  • index: It is the index of the current element which is being processed in the typedArray.
  • array: It is the typedArray which is being called.

Return value: It returns a new typedArray with the result of a provided function on each element of the given typedArray.

JavaScript code to show the working of this function:

Code #1:



filter_none

edit
close

play_arrow

link
brightness_4
code

<script>
   
  // Creating a typedArray with some elements
  const A = new Uint8Array([4, 9, 16, 25, 36]);
    
  // Calling map() function with the parameter
  // Math.sqrt function which find square root 
  // of the typedArray's elements
  const B = A.map(Math.sqrt);
  
  // Printing the result of the function
  document.write(B);
  
</script>

chevron_right


Output:

2,3,4,5,6

Code #2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<script>
   
  // Creating a typedArray with some elements
  var A = new Uint8Array([1, 2, 3, 4, 5, 6]);
    
  // Calling map() function
  var B = A.map(function(a) {
  return a * 5;
  });
    
  // Returning the results
  document.write(B);
  
</script>

chevron_right


Output:

5,10,15,20,25,30


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.