DEV Community

Cover image for JavaScript map() method
Sudhanshu Gaikwad
Sudhanshu Gaikwad

Posted on

JavaScript map() method

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

  1. Here's a simple map() example:
const numbers = [1, 2, 3, 4, 5];
const doubled = numbers.map(num => num * 2);

console.log(doubled);

// Output: [2, 4, 6, 8, 10]

Enter fullscreen mode Exit fullscreen mode
  1. Create a JSON File with Car Information and Display Using map()

First, create a JSON file named cars.json:

[
  {
    "name": "Toyota Camry",
    "model": "2023",
    "image": "https://example.com/toyota_camry.jpg"
  },
  {
    "name": "Honda Accord",
    "model": "2022",
    "image": "https://example.com/honda_accord.jpg"
  },
  {
    "name": "Tesla Model 3",
    "model": "2024",
    "image": "https://example.com/tesla_model_3.jpg"
  }
]

Enter fullscreen mode Exit fullscreen mode

create an HTML file index.html and use JavaScript to fetch and display the car information:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Car Display</title>
  <style>
    .car {
      border: 1px solid #ddd;
      padding: 10px;
      margin: 10px;
      text-align: center;
    }
    .car img {
      width: 100px;
      height: auto;
    }
  </style>
</head>
<body>
  <h1>Car Information</h1>
  <div id="car-container"></div>

  <script>
    // Here we have Fetch the car data
    fetch('cars.json')
      .then(response => response.json())
      .then(data => {
        const carContainer = document.getElementById('car-container');
        carContainer.innerHTML = data.map(car => `
          <div class="car">
            <h2>${car.name}</h2>
            <p>Model: ${car.model}</p>
            <img src="${car.image}" alt="${car.name}">
          </div>
        `).join('');
      })
      .catch(error => console.error('Error fetching the car data:', error));
  </script>
</body>
</html>

Enter fullscreen mode Exit fullscreen mode

Make sure to place the cars.json file in the same directory as your HTML file or adjust the fetch URL accordingly

Top comments (5)

Collapse
 
chaudharidevam profile image
Devam Chaudhari

Great article on the map() method ! . I wanted to mention that you can also use destructuring assignment to access the name , model , image properties directly. This way , you don’t have to write cars each time , making the code cleaner and more readable. For example:

cars.map(({name, model, image}) => { });

or
cars.map((cars) => {
const {name , model, image}= cars;
return ()
});

Thank you for the insightful content!

Collapse
 
sudhanshu_developer profile image
Sudhanshu Gaikwad

okay bother, thank you for recommending me.!

Collapse
 
syedmuhammadaliraza profile image
Syed Muhammad Ali Raza

👍

Collapse
 
andrew-saeed profile image
Andrew Saeed

Awesome guide, mate! Thanks so much!

Collapse
 
dag2no profile image
Daniel Uribe

ohhh muchas graciasss, ¡quería aprender a hacer eso!