DEV Community

Cover image for map() method explained : JS
Rahul
Rahul

Posted on • Updated on

map() method explained : JS

JavaScript methods are actions that can be performed on objects. So here is my new blog post I will cover the two JavaScript methods map() and filter(). Will explain easily and in detail.


map()

-> The map() method is used to apply a function on every element in an array and then return a new array.
Let's take a look at the syntax:-

let newArray = array.map( (v, i, a) => {
    // return element to newArray
});

// newArray - the new array that is returned
// array - the array to run the map function on
// v - the current value being processed
// i - the current index of the value being processed
// a - the original array
Enter fullscreen mode Exit fullscreen mode

The map() method can be thought of as for loop, that is specifically for transforming values. Let's take a look at this example:

let nums = [11, 12, 13, 14];
let minus10 = [];
for(let i = 0; i < nums.length; i++) {
    minus10[i] = nums[i] - 10;
 }
Enter fullscreen mode Exit fullscreen mode

The code results in a new array where each value of the old array is reduced by 10. Yes, it works, but there is an easier way to achieve the same result.

Now let's rewrite the previous function using the map() method.

let nums = [11, 12, 13, 14];
let minus10 = nums.map(value => value - 10);
  // minus10 = [1, 2, 3, 4]
Enter fullscreen mode Exit fullscreen mode

Just see. Without any loop

And using the arrow function we have a nice and clean function to create the same array. Because we only use the value, we only pass that to the map() method.

Here in this example, we'll utilize both the value and index arguments.

let nums = [11, 12, 13, 14];
let newArray = nums.map( (v, i) => {
  return {
     value: v,
     index: i
   };
});

// newArr = [
// {value: 11, index: 0},
// {value: 12, index: 1},
// {value: 13, index: 2},
// {value: 14, index: 3}
//   ]
Enter fullscreen mode Exit fullscreen mode

I think now you are seeing that whatever we return within the map() method is used to create the new array. You can use the current value, current index or even the entire array to determine what to return.



You could even do something stupid like this 😂😁 below and just return a static string :
let nums = [11, 12, 13, 14]; 
let cats = nums.map ( ( ) => 'cats');
 // cats = [`cat`, `cat`, `cat`, `cat` ]
Enter fullscreen mode Exit fullscreen mode

Till here, all the examples have transformed all of the values in the old array. Let's look at a way to transform some of the values in our array.

let nums = [11, 12, 13, 14]; 
ley newArr = nums.map(v => v % 2 === 0 ? v * 2 : v);
  // newArr = [11, 24, 13, 28];
Enter fullscreen mode Exit fullscreen mode

First, we checked if the value divided by two has a remainder of zero. If the remainder is zero we'll return v * 2 or double the current value.




I'm done. Thanks For Reading 😀😀

First Seen Here

Top comments (1)

Collapse
 
ozerhakan profile image
Hakan Özer

Hi Rahul,
Thanks for map article but I want to make a small fix :D

let nums = [11, 12, 13, 14];
let cats = nums.map ( ( ) => 'cats');
// cats = [cat, cat, cat, cat ] => returns cats :D