The
method map() of array always creates a new array without modify the original
array and the map method calls the provided function or execute the statement
for each array elements
You
can use map () method to iterate of each element of array instead of using JavaScript
looping function like for or foreach method and it will reduce number of lines
code to compare for or foreach method
Here
is an example of map method to calculate the square value for each element of
array
const sqrs = [1,2,3].map(x => x *
x);
console.log(sqrs);
Output:
Array [1, 4, 9]
another
example of map method to calculate the square root of each element in the array
const sqrs = [4, 9, 16, 25].map(Math.sqrt);
console.log(sqrs);
Output:
Array [2, 3, 4, 5]
Additional
Source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map
No comments:
Post a Comment