I'm learning react and I've got a problem displaying the key value pairs in JSX. An array of objects is shown below.
const heroSection = [
{
"Heading color": "#1F1235",
"Text color": "#1B1425",
"Button color": "#FF6E6C",
"Background color": "#F4EFFC",
"Accent text color": "#E2D9F1"
},
{
"Heading color": "#1F1235",
"Text color": "#1B1425",
"Button color": "#FF6E6C",
"Background color": "#F4EFFC",
"Accent text color": "#E2D9F1"
}
];
I'm trying to display key value pairs in JSX. My looping method.
HeroSectionColors.map(color => {
Object.keys(color).forEach(colorItem => {
console.log(colorItem, color[colorItem]);
});
});
In the console the key value pair is shown as I want to. I want to display the key value pairs in the JSX.
{HeroSectionColors.map(color => {
Object.keys(color).forEach(colorItem => (
<button className="button-hues">
<span className="hues-display"></span>
<div className="hues-info">
<span className="section-color-name">{colorItem}</span>
<span className="color-hex">{color[colorItem]}</span>
</div>
</button>
));
})}
The above code does not work and renders nothing in the browser. Any help is appreciated. P.S. Apologies for any formatting errors.
Array.forEach()is used for side effects, and doesn't return anything. UseArray.map()instead. Also check outObject.entries(), which is more fitting for this case thanObject.keys(). - Ori Drori<div>inside<button>is a funny and absolutely invalid markup. - Roko C. Buljan