I want to use a dictionary of HTML color codes and then map over that dictionary for styles.
For example:
const color = {
red: "#E53D25",
lightred : "#ED7462"
...
}
Here is my mapping function; I use index to iterate through the CSS values inside the dictionary above.
.map((stock, index) => (
<Grid>
<ListItem>
<ListItemAvatar>
<Avatar style={{ backgroundColor: color[index]}}>
{index + 1}
</Avatar>
</ListItemAvatar>
<ListItemText
primary={stock.symbol}
secondary={financialData && financialData[5].filter((i) => i.symbol === stock.symbol).map(
filteredSymbol => (
<>
{filteredSymbol.company}
</>
))}
/>
</ListItem>
</Grid>
I try to iterating through the dictionary like this and apply color by index: style={{ backgroundColor: color[index]}}
But this does not work. How can I accomplish what I'm trying to build?
indexis a number, socolor[index]it will returnedundefinedsince yourcolorobject is keyed by a color string name. What is the expected relationship betweenindexand the color name then? If you just want the color by insertion order, then you can doObject.values(color)[index]