DEV Community

Cover image for Map- One Of The Most Important Array Methods In Javascript
Emor Musk
Emor Musk

Posted on

Map- One Of The Most Important Array Methods In Javascript

To begin with, what is an array? An array refers to a data structure used to collectively group elements. These elements could be of the same or different data types like string, number, boolean, etc. In other words, think of an array as a container for grouping elements. Arrays are useful for maintaining and organizing data elements clearly and efficiently.

//array containing elements of the same data type

let array_of_numbers=[1,2,3,4,5]

//array containing elements of different data types

let arr=[7,'Sam',True, undefined]
Enter fullscreen mode Exit fullscreen mode

A method is a function attached to an object. Methods in Javascript are called with the dot operator. Now, under the hood, everything in Javascript is an object and has methods. The data type string has several methods like the length, toUpperCase, indexOf, etc. Array methods are functions peculiar to arrays that allow us to perform some tasks on arrays. Some of the array methods include sort, reduce, filter, etc.

Map Method

The map method is an array method that is used for iterating over the elements in the array and returning a new array. The map method can take in three parameters: the element, index, and array. The element is each element of the array being iterated on, the index is the index of each element of the array being iterated on, and the array is the array itself being iterated on.

let ages=[22,20,21,19,18,16,21]
//For the map method, three parameters can be taken in, the element, index, and the array itself

const newAges= ages.map((element,index,array)=>{
    return element + 2
})
console.log(newAges)

//The answer would be [24, 22, 23, 21,20, 18, 23]

const newAgesIndex= ages.map((element,index,array)=>{
    return index 
})
console.log(newAgesIndex)


//The answer would be [2,3,4,5,6,7]
Enter fullscreen mode Exit fullscreen mode

Categorically speaking, one of the most important features of the map method is the return value. Mapping over an array returns a new array without modifying the original array, hence, the array mapped over is left untouched and you have a new array based on the condition stated for the mapping. This concept supports the principle of immutability in programming, making it easier to avoid side effects, and also provides a clean and functional programming approach.

Furthermore, in libraries like React, the return value from the map method becomes very essential. React uses a component-based architecture, which simply involves breaking the webpage into components. When building out web pages, some components are repeated across the page, so it is always a safe practice to create a custom reusable component for them. This custom reusable component can not just be repeated numerous times in the code, that is redundant code. An array is created to store the data that should be in each component, this array is then mapped over and returns the custom component.

import React from 'react'

const File = () => {

    //array containing the recipes
    const recipes=['curry', 'thyme', 'onions', 'salt']

    const newArray= recipes.map(element=>{
        return <Recipe key={element} /> //Recipe is the custom reusable component 
        //the key property is meant to be a unique value for each iteration
    })

  return (
    <div>
      {newArray}
    </div>
  )
}

export default File
Enter fullscreen mode Exit fullscreen mode

Moreover, when building a dynamic web page, data from an endpoint is required. Data gotten from endpoints could be of any data type, it could be a string, number, or object but more often than not they come in the form of arrays. Now, when this data in the array is to be rendered in the markup dynamically, the map method comes into play. The data in the array gets mapped over and for each element in the array, the component or what is to be returned gets returned in the markup.

The forEach method is often mistaken for the map method because it can take in the same parameters as the map method — element, index, and the array itself. They both don’t modify the original array. Still, the map method returns a new array after mapping over the original array. In contrast, the forEach method executes a provided function once for each array element and does not return a new array.

const ages= [8,9,10,6,7]

ages.forEach((element,index,array)=>{
    console.log(element+2)
})

//Notice that it does not return a new array, rather it works on the array elements based on the condition stated 
//The answer would be [10,11,12,8,9]
Enter fullscreen mode Exit fullscreen mode

Map- The Most Important Array Method?

An array is a beneficial and important data type in Javascript because it sequentially allows for data organization. This data organization is needed in web development because, at times, the order of the data needs to be ensured and an array is perfect for that. All array method have their purposes and perfect use cases. There is no single array method that does not have a role it plays.

In summary, the map method is excellent for transforming data within arrays. It allows for easy transformation of each data based on certain criteria and also returns a new array. This is beneficial in web development because of its return value after the iteration which is later used to render elements on the markup or for subsequent coding.

Top comments (0)