DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Hoisting in JavaScript
  • TypeScript: Useful Features
  • Using Environment Variable With Angular
  • useState() vs. useRef(): Understand the Technical Difference

Trending

  • Ujorm3: A New Lightweight ORM for JavaBeans and Records
  • OpenAPI From Code With Spring and Java: A Recipe for Your CI
  • Catching Data Perimeter Drift Before It Reaches Production
  • No More Cheap Claude: 4 First Principles of Token Economics in 2026
  1. DZone
  2. Data Engineering
  3. Data
  4. A Comprehensive Guide on JavaScript Array Map Method

A Comprehensive Guide on JavaScript Array Map Method

Discover everything you need to know about the JavaScript Array Map Method in one easy-to-follow guide covering syntax, applications, and more!

By 
Janki Mehta user avatar
Janki Mehta
·
Mar. 01, 23 · Tutorial
Likes (2)
Comment
Save
Tweet
Share
2.9K Views

Join the DZone community and get the full member experience.

Join For Free

In JavaScript, arrays are a useful way to store and manipulate data. The map() method is a built-in function that allows you to iterate over each element in an array and apply a function to it. The map() method creates a new array with the results of calling a provided function on every element in the calling array. In this article, we will dive into the map() method and explore its syntax, functionality, and use cases. 

Syntax

The map() the method is called on an array and takes a callback function as its argument. The callback function takes three parameters: the current element being processed, the index of the current element, and the array that map() was called upon. The syntax or commands for the map() method is as follows:

 
array.map(callback(currentValue, index, array))


Here array is the array that the map() method is being called on. callback is the function to be called on each element of the array. currentValue represents the value of the current element being processed. index represents the index of the current element being processed. array represents the original array. 

Functionality

The map() method creates a new array with the results of calling the callback function on each element in the original array. The original array is not modified, and the new array is returned as the result of the map() method. The length of the new array will be the same as the length of the original array. If the callback function returns, the value is not included in the new array.

The map() method is a higher-order function, which means it takes a function as an argument or returns a function as its result. The map() method is a pure function, which means it does not modify the original array and has no side effects.

Here are some examples to illustrate the use of the map() method:

1. Doubling the Elements of an Array

 
const numbers = [1, 2, 3, 4, 5];
const doubledNumbers = numbers.map(number => number * 2);
console.log(doubledNumbers); 
// Output: [2, 4, 6, 8, 10]


Here, the map() method is used to create a new array doubledNumbers with the elements of the original array numbers doubled. 

2. Converting Fahrenheit to Celsius

 
const fahrenheitTemperatures = [32, 68, 86, 104];
const celsiusTemperatures = fahrenheitTemperatures.map(temp => (temp - 32) * 5 / 9);
console.log(celsiusTemperatures); 
// Output: [0, 20, 30, 40]


Here, the map() method is used to convert an array of Fahrenheit temperatures to Celsius temperatures. 

3. Extracting Properties From Objects in an Array

 
const people = [
  { name: "Alice", age: 23 },
  { name: "Bob", age: 27 },
  { name: "Charlie", age: 18 },
];
const names = people.map(person => person.name);
console.log(names); 
// Output: ["Alice", "Bob", "Charlie"]


Here, the map() method is used to extract the name property from each object in the people array. 

4. Flattening an Array of Arrays

 
const nestedArray = [[1, 2], [3, 4], [5, 6]];
const flattenedArray = nestedArray.map(innerArray => innerArray.join(","));
console.log(flattenedArray); 
// Output: ["1,2", "3,4", "5,6"]


Here, the map() method is used to concatenate the elements of each nested array in the nestedArray array into a string with commas. 

Conclusion

In conclusion, the map() method is a powerful tool for working with arrays in JavaScript. It allows you to transform each element of an array using a callback function and create a new array with the transformed values. The map() method is a higher-order function that takes a function as an argument and is a pure function that does not modify the original array.

Some common use cases for the map() method include transforming data, extracting properties from objects in an array, and flattening arrays of arrays. By using the map() method, you can write more concise and expressive code that is easier to read and maintain.

It's important to note that the map() method is not the only array method available in JavaScript. Other array methods, such as filter(), reduce(), and forEach(), can also be useful for different tasks. 

Overall, understanding and utilizing the map() method is essential for any JavaScript developer who wants to work efficiently and effectively with arrays.

Data structure JavaScript Array data type Function type Command (computing)

Opinions expressed by DZone contributors are their own.

Related

  • Hoisting in JavaScript
  • TypeScript: Useful Features
  • Using Environment Variable With Angular
  • useState() vs. useRef(): Understand the Technical Difference

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook