How to display keys and values of objects in a loop in React?
Loop through object keys and values using Object.entries() and map() to render data dynamically in React.
Advertisement
Question
How to display keys and values of objects in a loop in React?
Answer
You can’t map directly over an object — first, convert it into an array using Object.entries().
function UserDetails() {
const user = { name: 'Ghazi', role: 'Engineer', country: 'India' };
return (
<ul>
{Object.entries(user).map(([key, value]) => (
<li key={key}>
<strong>{key}:</strong> {value}
</li>
))}
</ul>
);
}
âś… Other useful methods:
Object.keys(obj)→ returns an array of keysObject.values(obj)→ returns an array of values
Real-World Example
Rendering user profiles, config summaries, or any object-based data structure.
Quick Practice
Create a component that displays all properties of a product object (name, price, stock) using Object.entries().
Summary
Use Object.entries() + map() to render object keys and values cleanly in React.
Can I loop objects directly in JSX?
Not directly. Convert them to arrays first using Object.keys, Object.values, or Object.entries.
Advertisement
Stay Updated
Get the latest frontend challenges, interview questions and tutorials delivered to your inbox.
Advertisement