Course contentsShow
← React
Lesson 143 of 2,3965. Lists and KeysFree lesson

Accessing Item Data in .map()

How to destructure item properties and use them within the mapped JSX elements.

What You'll Learn

You'll learn how to pull out and use individual properties from each item as you loop through an array with .map().


The Core Idea

When you call .map() on an array, the callback function receives each item as an argument. That item is typically an object with multiple properties. You can access those properties using dot notation or destructure them right in the function parameter for cleaner code.

Think of it like unpacking a box: you can either grab the whole box and pull items out one by one (item.name, item.price), or you can open the box immediately and lay everything out on the table ({ name, price }).


Two Ways to Access Item Data

Dot Notation:

const products = [
  { id: 1, name: 'Laptop', price: 999 },
  { id: 2, name: 'Mouse', price: 25 }
];

const listItems = products.map((product) => (
  <li key={product.id}>
    {product.name} - ${product.price}
  </li>
));

Destructuring in the Parameter:

const listItems = products.map(({ id, name, price }) => (
  <li key={id}>
    {name} - ${price}
  </li>
));

Both approaches work identically. Destructuring is cleaner when you use multiple properties, but dot notation is fine for simpler cases or when you only need one or two fields.


When to Use Each

  • Dot notation: Quick access, fewer properties, or when the item name adds clarity (user.email is self-documenting).
  • Destructuring: Multiple properties, cleaner JSX, less repetition.

Key Takeaway: Use dot notation or destructure the item parameter in .map() to extract and display individual properties in your JSX—choose whichever keeps your code readable.