Lists of Objects in React
Learn how to render arrays of objects in React using the map() method and display object properties.
1. Introduction to Lists of Objects
In React, an array can contain objects instead of simple values. Each object can store multiple properties, such as an ID, name, price, or category.
We can use the JavaScript map() method to iterate through an array of objects and display their properties inside JSX elements.
2. Understanding an Array of Objects
Consider the following array of student objects. Every object contains an ID and a name.
const students = [
{ id: 1, name: "Amit" },
{ id: 2, name: "Priya" },
{ id: 3, name: "Rahul" }
];
The students variable contains three objects.
We can access their properties using expressions such as
students[0].name and
students[1].name.
Example Properties
| ID | Name |
|---|---|
| 1 | Amit |
| 2 | Priya |
| 3 | Rahul |
3. How Rendering Lists of Objects Works
The following diagram explains how React transforms an array of objects into rendered list elements.
4. Rendering a List of Students
In this example, map() processes every student object and displays the student's name in an unordered list.
Example
function StudentList() {
const students = [
{ id: 1, name: "Amit" },
{ id: 2, name: "Priya" },
{ id: 3, name: "Rahul" }
];
return (
<div>
<h2>Student List</h2>
<ul>
{students.map((student) => (
<li key={student.id}>
{student.name}
</li>
))}
</ul>
</div>
);
}
export default StudentList;
Expected Output
Student List
- Amit
- Priya
- Rahul
student.name displays each student's name,
while student.id provides the key for that
list item.
5. Displaying Multiple Object Properties
We can display more than one property from an object. For example, we can show a student's ID, name, and course.
Example
function StudentDetails() {
const students = [
{ id: 1, name: "Amit", course: "React" },
{ id: 2, name: "Priya", course: "Angular" },
{ id: 3, name: "Rahul", course: "ASP.NET Core" }
];
return (
<div>
<h2>Student Details</h2>
{students.map((student) => (
<div key={student.id}>
<p>ID: {student.id}</p>
<p>Name: {student.name}</p>
<p>Course: {student.course}</p>
</div>
))}
</div>
);
}
export default StudentDetails;
Expected Output
Student Details
ID: 1
Name: Amit
Course: React
ID: 2
Name: Priya
Course: Angular
ID: 3
Name: Rahul
Course: ASP.NET Core
6. Example: Rendering Product Objects
The same approach can be used to display products. Each product object contains an ID, name, and price.
Example
function ProductList() {
const products = [
{ id: 101, name: "Keyboard", price: 800 },
{ id: 102, name: "Mouse", price: 500 },
{ id: 103, name: "Monitor", price: 7000 }
];
return (
<div>
<h2>Product List</h2>
<ul>
{products.map((product) => (
<li key={product.id}>
{product.name} - ₹{product.price}
</li>
))}
</ul>
</div>
);
}
export default ProductList;
Expected Output
Product List
- Keyboard - ₹800
- Mouse - ₹500
- Monitor - ₹7000
7. Using Destructuring with map()
JavaScript destructuring lets us extract object properties directly into variables. This can make list-rendering code shorter and easier to read.
Using Dot Notation
{students.map((student) => (
<li key={student.id}>
{student.name}
</li>
))}
Using Destructuring
{students.map(({ id, name }) => (
<li key={id}>
{name}
</li>
))}
Both examples render the student names. In the second
example, id and name are extracted
from each object directly in the callback parameter.
8. Common Mistakes
Mistake 1: Displaying the Entire Object
{students.map((student) => (
<p key={student.id}>{student}</p>
))}
A plain JavaScript object cannot be rendered directly as
a React child. Access a specific property such as
student.name instead.
Mistake 2: Forgetting the Key
{students.map((student) => (
<li>{student.name}</li>
))}
Provide a suitable key for every sibling element returned from the map() callback.
Mistake 3: Using a Property That Does Not Exist
{students.map((student) => (
<li key={student.id}>
{student.fullName}
</li>
))}
If the objects contain a name property instead
of fullName, the displayed value will be
undefined. Use the property name defined in the data.
9. Practice Tasks
Complete these tasks to practise rendering arrays of objects.
Summary
Arrays of objects allow React applications to work with structured data containing multiple properties. The map() method can iterate through these arrays and return JSX elements for each object. Dot notation or destructuring can be used to access properties such as ID, name, course, and price. Each sibling element should have a suitable key, preferably a stable and unique ID from the data.