Skip to content

Instantly share code, notes, and snippets.

@Daniel-Walsh
Last active October 1, 2021 02:28
Show Gist options
  • Save Daniel-Walsh/4e38ceb2ab587b09fc9072619f614066 to your computer and use it in GitHub Desktop.
Save Daniel-Walsh/4e38ceb2ab587b09fc9072619f614066 to your computer and use it in GitHub Desktop.
Loop through and render Object key/values with React JSX #react #jsx #objects
// Create an Array from the object's keys, then use .map() to loop through each
// key and return your new Array of JSX elements for rendering. Object values
// can be accessed using bracket notation.
const RenderObjectKeyValues = () => {
const recipeTags = {
asian: 1,
beef: 2,
breakfast: 2,
cakes: 2,
chicken: 2,
};
return (
<ul>
{Object.keys(recipeTags).map((key) => (
<li>
{key} ({recipeTags[key]})
</li>
))}
</ul>
);
};
export default RenderObjectKeyValues;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment