August 11, 2017

[Code] | Javascript map() - Functional Programming


map calls a provided callback function once for each element in an array, in order, and constructs a new array from the results. callback is invoked only for indexes of the array which have assigned values, including undefined.




1) The map() method creates a new array with the results of calling a provided function on every element in the calling array.



2) map does not mutate the array on which it is called (although callback, if invoked, may do so).

Where to use?
- Loop the value
- Modify the array
- apply the Javascript method
- string operation and many more

Syntax:



How to create a new list (by for loop as well as by map())



Using Javascript Method:



Advantage:
- As you can see above map() is making your code line less
- with map() you do awesome things just as we did with Math.sqrt
- map() also give you new array as result without changing old array.

Codelink 

Happy Learning!!

No comments: