Array.map() function in javascript

Array.map() function in javascript

What does map() function of Array in javascript do?

Simple, the map function creates a new array with the results of calling a callback for every element in the array.

Example:

const array1 = [1, 2, 3];

//mulyiply every element by 2 and create a new array with the newly created elements

const multipliedArrayBy2 = array1.map(element => element * 2);

//Result? multipliedArrayBy2 will have the value [2, 4, 6]

You can also use map function with the current index of the element at we are at.

Example:

const array1 = [1, 2, 3]

//mulyiply every element by it’s current index and create a new array with the newly created elements

const multipliedArrayByIndex = array1.map((element, index) => element * index);

//Result? //multipliedArrayByindex will have the value [0, 2, 6]

That’s all you have to know about map function to be productive.

If you want to read more about Array.map() function you can do it here.

Enjoy!

Related Posts