object in map using react js code example
Example 1: how to map array of objects in react
[{
name: 'Sam',
email: '[email protected]'
},
{
name: 'Ash',
email: '[email protected]'
}
].map( ( {name, email} ) => {
return <p key={email}>{name} - {email}</p>
})
Example 2: new Map() collection in react state
function MapComponent(){
const [myMap, setMyMap] = useState(new Map());
const updateMap = (k,v) => {
setMyMap(new Map(myMap.set(k,v)));
}
return(
<ul>
{[...myMap.keys()].map(k => (
<li key={k}>myMap.get(k)</li>
))}
</ul>
);
}