filter in array and return value reactjs code example
Example 1: 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 2: filter() array of objects on change react
const handleChange = (e) => {
const id = hospitalsDetails.filter(obj => obj.hospitalName == e.target.value)
setCurrentApp({ ...currentApp, [e.target.id]: e.target.value, ["hospitalID"]: id.hospitalID })
}