Functions
Map
- The map() method creates a new array by calling a provided function on every element in the calling array.
- used to traverse and display a list of similar objects of a component
const [monsters, setMonsters] = useState[{id:1,name:'Linda'},{id:2,name:'Jacky'},{id:3,name:'Tom'}];
return(
{this.state.monsters.map((eachone)=>{
return <h1>{eachone.name}</h1>
})}
);
Filter
- The filter() method create a new array by looping through an array and including or excluding elements based on provided condition
const names = ['James', 'John', 'Paul', 'Ringo', 'George'];
<div>
{names.filter(name => name.includes('J')).map(filteredName => (
<li>
{filteredName}
</li>
))}
</div>
Reduce
- The reduce() method return the sum of all the elements in an array
const arr = [1,2,3,4,5];
const sum = arr.reduce((total,item)=>total+item,0);