Last updated: Apr 6, 2024
Reading timeยท5 min
To use a condition inside map() in React:
map()
method on an array.if
condition that explicitly returns if it's satisfied.null
to render nothing.export default function App() { const arr = [1, 2, 3, 4, 5, 6]; return ( <div> {arr.map((element, index) => { if (element % 2 === 0) { return <h2 key={index}>{element}</h2>; } return <h2 key={index}>X</h2>; })} </div> ); }
We used the Array.map() method to iterate over an array.
map()
gets called with each element in the array and the index of the current iteration.On each iteration, we check if the element is divisible by 2
and if it is, we
return the element, otherwise, we return X
.
If you get the error that map() is not a function in React, click on the link and follow the instructions.
null
if you don't need to render anythingIf you want to render nothing in the else
clause, you can return null
.
export default function App() { const arr = [1, 2, 3, 4, 5, 6]; return ( <div> {arr.map((element, index) => { if (element % 2 === 0) { return <h2 key={index}>{element}</h2>; } // ๐๏ธ Render nothing return null; })} </div> ); }
The example renders the numbers that are divisible by 2
, otherwise renders
nothing.
Alternatively, you can use a ternary operator.
This is a three-step process:
map()
method on an array.export default function App() { const arr = [1, 2, 3, 4, 5, 6]; return ( <div> {arr.map((element, index) => { return element % 2 === 0 ? ( <h2 key={index}>{element}</h2> ) : ( <h2 key={index}>X</h2> ); })} </div> ); }
We used a
conditional ternary operator
which is very similar to an if/else
statement.
In other words, if the element is divisible by 2
, we return the element,
otherwise, return X
.
Like in the previous example, if you want to return nothing in the else
clause, you have to return null
.
export default function App() { const arr = [1, 2, 3, 4, 5, 6]; return ( <div> {arr.map((element, index) => { return element % 2 === 0 ? <h2 key={index}>{element}</h2> : null; })} </div> ); }
We used the index
for the key
prop in the examples, however, it's better to
use a stable unique identifier if you have one.
The key
prop is used internally by React for performance reasons. It helps the
library make sure to only re-render the array elements that have changed.
Having said that, you won't see any noticeable difference unless you're dealing with many thousands of array elements.
Make sure you don't have duplicate keys, otherwise, you'll get the Encountered two children with the same key warning.
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.
To break a map() loop:
slice()
method on the array to get a portion of the array.map()
method on the portion of 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: 'Delilah', country: 'Denmark'}, {id: 5, name: 'Ethan', country: 'Egypt'}, ]; // ๐๏ธ map() first 2 elements of array return ( <div> {employees.slice(0, 2).map((employee, index) => { return ( <div key={index}> <h2>name: {employee.name}</h2> <h2>country: {employee.country}</h2> <hr /> </div> ); })} </div> ); }
The Array.slice() method doesn't modify the original array, instead, it creates a new array (a shallow copy of the original array).
We passed the following 2 parameters to the slice()
method:
Name | Description |
---|---|
startIndex | The index of the first element to be included in the new array |
endIndex | Go up to, but not including this index |
We specified a start index of 0
and an end index of 2
, so we got a portion
of the array with elements 0
and 1
.
end index
you provide to the Array.slice
method exceeds the array's length, the method does not throw an error but returns all array elements.const arr = ['a', 'b', 'c']; const first100 = arr.slice(0, 100); console.log(first100); // ๐๏ธ ['a', 'b', 'c']
We tried to get the first 100
elements of an array, which only contains 3
elements.
As a result, the new array contains all 3
elements of the original array.
filter()
You can also use the
Array.filter()
method before calling map()
.
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: 'Delilah', country: 'Denmark'}, {id: 5, name: 'Ethan', country: 'Egypt'}, ]; // ๐๏ธ map() LAST 2 elements of array return ( <div> {employees .filter(employee => { return ( employee.country === 'Belgium' || employee.country === 'Denmark' ); }) .map((employee, index) => { return ( <div key={index}> <h2>name: {employee.name}</h2> <h2>country: {employee.country}</h2> <hr /> </div> ); })} </div> ); }
The function we passed to the filter()
method gets called with each element in
the array.
On each iteration, we check if the current object has a country
property equal
to Belgium
or Denmark
and return the result.
filter()
method returns an array containing only the elements for which the callback function returns a truthy value.In the example, the map()
method is only called with the objects with id
of
2
and 4
.