How to render and item once In a map function ? I have and array of objects and I want to render the color value only once into another component , I tried to push the color in to an array and checked against it but since it is inside the loop it is getting added instantly,and this check will fail, the point is I don’t want the same color to be rendered twice, if found then only once… any good solution for this case ?
const items =[{car : 'bmw', color : 'black'}, {car: 'opel', color :'black'}, {car:'landrover',color:'red'}]
{items.map((item, i) => {
let arr = [];
//arr.push(items[i].color);
return (
<React.Fragment>
{arr.includes(items[i]) ? undefined : <colors img={items[i]?.color} /> }
<items key={item.id} Car={item.car} />
</React.Fragment>
);
})}
// Expected result
// black color component
// 'bmw'
// 'opel'
// red color component
// 'landrover'