Last updated: Apr 6, 2024
Reading time·3 min
To loop through an array of objects in React:
map()
method to iterate over the array.map()
gets called for each element in the array.export default function App() { const employees = [ {id: 1, name: 'Alice', country: 'Austria'}, {id: 2, name: 'Bob', country: 'Belgium'}, {id: 3, name: 'Carl', country: 'Canada'}, {id: 4, name: 'Dean', country: 'Denmark'}, {id: 5, name: 'Ethan', country: 'Egypt'}, ]; return ( <div> {employees.map(employee => { return ( <div key={employee.id}> <h2>name: {employee.name}</h2> <h2>country: {employee.country}</h2> <hr /> </div> ); })} </div> ); }
The function we passed to the Array.map() method gets called for each element in the array.
On each iteration, we set the key
prop on the outermost element to a unique
value, and rendered the values of the object.
Setting the key
prop to a unique value is important, otherwise, you'd get the
Encountered two children with the same key
warning.
You can also destructure the object's properties to make your code a little easier to read.
export default function App() { const employees = [ {id: 1, name: 'Alice', country: 'Austria'}, {id: 2, name: 'Bob', country: 'Belgium'}, {id: 3, name: 'Carl', country: 'Canada'}, {id: 4, name: 'Dean', country: 'Denmark'}, {id: 5, name: 'Ethan', country: 'Egypt'}, ]; return ( <div> {employees.map(({id, name, country}) => { return ( <div key={id}> <h2>name: {name}</h2> <h2>country: {country}</h2> <hr /> </div> ); })} </div> ); }
We destructured the parameters of the object parameter, so we don't have to access each property on the object.
Note that you shouldn't try to access the key
property, otherwise, the warning
Key is not a prop. Trying to access it will result in undefined
is raised.
Alternatively, you can use the Array.forEach method.
forEach()
This is a three-step process:
forEach()
method to iterate over the array.export default function App() { const employees = [ {id: 1, name: 'Alice', country: 'Austria'}, {id: 2, name: 'Bob', country: 'Belgium'}, {id: 3, name: 'Carl', country: 'Canada'}, {id: 4, name: 'Dean', country: 'Denmark'}, {id: 5, name: 'Ethan', country: 'Egypt'}, ]; const results = []; employees.forEach(employee => { results.push( <div key={employee.id}> <h2>name: {employee.name}</h2> <h2>country: {employee.country}</h2> <hr /> </div>, ); }); return ( <div> {results} </div> ); }
This example achieves the same result.
forEach()
method gets called with each element (object) in the array.Instead of directly rendering the object's values, we push the JSX markup for
each object into a results
array.
The last step is to render the array of results.
If you need to filter an array of objects in React, click on the following article.
for...of
You can also loop through an array of objects using the for...of loop.
export default function App() { const employees = [ {id: 1, name: 'Alice', country: 'Austria'}, {id: 2, name: 'Bob', country: 'Belgium'}, {id: 3, name: 'Carl', country: 'Canada'}, {id: 4, name: 'Dean', country: 'Denmark'}, {id: 5, name: 'Ethan', country: 'Egypt'}, ]; const results = []; for (const employee of employees) { results.push( <div key={employee.id}> <h2>name: {employee.name}</h2> <h2>country: {employee.country}</h2> <hr /> </div>, ); } return ( <div> {results} </div> ); }
The for...of
loop can also be used to iterate over an array of objects.
for...of
instead of the forEach()
method when you have to use the break
keyword to exit out of a loop prematurely.The break
keyword cannot be used in the forEach()
method, but it is
supported in the for...of
loop.
I've also written an article on:
If you need to loop through an object, click on the link and follow the instructions.