how to use filter function in react js code example
Example 1: how the filter() function works javascript
const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9];
const filter = arr.filter((number) => number > 5);
console.log(filter);
Example 2: filter array react
<div>
{people.filter(person =>
person.age < 60).map(filteredPerson => (
<li>
{filteredPerson.name}
</li>
))}
</div>
Example 3: react filter
import React from 'react';
const people = [
{
name: 'James',
age: 31,
},
{
name: 'John',
age: 45,
},
{
name: 'Paul',
age: 65,
},
{
name: 'Ringo',
age: 49,
},
{
name: 'George',
age: 34,
}
];
function App() {
return (
<div>
{people.filter(person => person.age < 60).map(filteredPerson => (
<li>
{filteredPerson.name}
</li>
))}
</div>
);
}
export default App;
Example 4: render a list using array filter react
react rendering using array.filter