My requirement i have a json object list using that i am checking for a name in json and if the name exist i am displaying a image from local assets folder. my code is below
After .map im displaying data like below
<td>
<div>
<img
src={
DynamicData.CompanyName.includes("Lenovo")
? LenovoImage
: DynamicData.CompanyName.includes("Dell")
? DellImage
: null
}
alt="img"
className="DataLogo"
/>
</div>
</td>
So here how can i display if i have more than 2 options is this approach is good ? are there any other approach ?
If there are many values to check, this looks more convenient.
const imagesMapping = {
Lenovo: LenoviImage,
Dell: DellImage,
// other values
}
const company = Object.keys(imagesMapping).find(name =>
DynamicData.CompanyName.includes(name));
const image = company ? imagesMapping[company] : null; // instead of null better use a DefaultImage
const result = <img src={image} alt="img" className="DataLogo" />