The JavaScript Array Map() Method

Sarah Chima - Dec 22 '18 - - Dev Community

Ever wondered what the JavaScript array map method is? what it can do? or when it should be used? This is article is for you. Before we go any further, let us get a formal definition of the map method.

Meet the method

According to MDN documentation:

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

Let us simplify this.

The JavaScript map method takes an array(Array 1) and creates a new array(Array 2) by calling a function on each element in the given array(Array 1). To explain this further, we will use an example to see what the map method accomplishes.

Let us use our regular for-loop to show that the map function does. A classic example is if we want an array of the square of all elements in a given array, we could do this using a for-loop as seen in the example below:

    const array = [2, 5, 9];
    let squares = [];

    for (let i = 0; i < array.length; i++) {
        squares.push(array[i] * array[i]));
    }

    console.log(squares); // [4, 25, 81]
    console.log(array); // [2, 5, 9]

Enter fullscreen mode Exit fullscreen mode

What does the above achieve? It loops through an array, finds the square of each element in the array and pushes it to the squares array that was earlier defined.

This is similar to what the map function achieves only that you do not need to define another array that the result is pushed to. The map function automatically does that. So let us accomplish the above using the map function.

P.S: Arrow functions are used in the example. If you do not fully understand its syntax, please refer to this article on it.

    const array = [2, 5, 9];
    let squares = array.map((num) => num * num);

    console.log(squares); // [4, 25, 81]
    console.log(array); // [2, 5, 9]
Enter fullscreen mode Exit fullscreen mode

Notice how much easier it to use the map method and still accomplish the same thing. Also, note that the initial array remains the same, this is especially useful in functional programming. Now let us dig a little deeper into the map method.

The Map Method and its Syntax

The syntax of the map function is as follows:

    let newArray = array.map((currentValue, index, array) => {
        // return element to new Array
    });
Enter fullscreen mode Exit fullscreen mode
  • newArray - the array that is returned
  • array - the array on which the map method is called
  • currentValue - the value being processed
  • index - the index of the current value being processed
Here are some things to note about the map method:
  1. It returns a new array.
  2. It does not mutate the original array on which it was called i.e the original array stays the same.
  3. The range of element processed by the map function is set before the first invocation. If elements are added to the array after the map begins, it will not be processed by the callback.

When To use the Map Method

Given that there are other similar array methods like the ForEach method, you might wonder, "when should I use (or not) the map method?" Here are some questions that can help you decide:

  1. Do I need an array to be returned from the method and will the returned array be used?
  2. Am I returning a value from the callback function?

If your answer to any of these questions is Yes, you should use the map function. If your answer is negative in both cases, you should probably use forEach or for..of instead.

Examples of the Map Method

In addition to the example used before, here are some other examples of things you can do with the map method.

Example 1: Extracting values from an array of objects

We want to extract certain values from an array of objects. For instance, in a array of girls, we want to get the ages of the girls.

    const girls = [
       {name: 'Sarah', age: 19},
       {name: 'Laura', age: 10},
       {name: 'Jessy', age: 29},
       {name: 'Amy', age: 23}];

    let girlsAges = girls.map((girl) => girl.age);

    console.log(girlsAges);  //[19, 10, 29, 23]

Enter fullscreen mode Exit fullscreen mode
Example 2: Apply the Callback on only certain elements

If we want to the callback to only be applied to certain elements in an array, say odd numbers, we can use an if statement to do this.

    const numbers = [4, 9, 36, 49];

    let oddSquareRoots = numbers.map((num) => {
       if(num % 2 != 0) {
           return Math.sqrt(num)     
       }
       return num;
    })

    console.log(oddSquareRoots);
Enter fullscreen mode Exit fullscreen mode

or using ternary operators

    const numbers = [4, 9, 36, 49];

    let oddSquareRoots = numbers.map((num) => {
       return num % 2 !== 0 ? Math.sqrt(num) : num 
    })

    console.log(oddSquareRoots);
Enter fullscreen mode Exit fullscreen mode

However, a more efficient way to achieve this is using the JavaScript Array Filter method. This will be discussed in my next post.

Conclusion

The JavaScript Array Map method is a method that can be used to simplify your code if used correctly. If you have other examples to show how you use the map method, please share them in the comment section.

Got any question or addition? Please leave a comment.

Thank you for reading :)

Shameless Plug🙈

If you want to know more about me, here's a link to my website.

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Terabox Video Player