![]() |
VOOZH | about |
The Array.map() is an inbuilt TypeScript function that creates a new array with the results of calling a provided function on every element in the array.
array.map(callback[, thisObject])This method accepts two parameters as mentioned above and described below:
This method returns the created array.
Example 1: Applying a Mathematical Function: In this example we creates a number[] array, applies Math.log to each element using map().
Output:
[
2.3978952727983707,
4.48863636973214,
3.1354942159291497,
1.9459101490553132,
4.584967478670572
]
Example 2: Mapping Elements with Their Indices: In this example we uses map() to iterate over arr, squaring each value. For each element.
Output:
key : 0 value : 4
key : 1 value : 25
key : 2 value : 36
key : 3 value : 9
key : 4 value : 64
key : 5 value : 81