When calling Object.keys it returns a array of the object's keys.

Object.keys({ test: '', test2: ''}) // ['test', 'test2']

When you call Array#map the function you pass will give you 2 arguments;

  1. the item in the array,
  2. the index of the item.

When you want to get the data, you need to use item (or in the example below keyName) instead of i

{Object.keys(subjects).map((keyName, i) => (
    <li className="travelcompany-input" key={i}>
        <span className="input-label">key: {i} Name: {subjects[keyName]}</span>
    </li>
))}
Answer from TryingToImprove on Stack Overflow
🌐
React
react.dev › learn › rendering-lists
Rendering Lists – React
How to render components from an array using JavaScript’s map() How to render only specific components using JavaScript’s filter() ... Say that you have a list of content. ... The only difference among those list items is their contents, their data. You will often need to show several instances of the same component using different data when building interfaces: from lists of comments to galleries of profile images. In these situations, you can store that data in JavaScript objects and arrays and use methods like map() and filter() to render lists of components from them.
Discussions

How to render an array of objects with Array.map in React
Array.map does not exist. The builtin Array does not have such a method. The only static methods which are defined are Array.from Array.isArrax and Array.of. What you are refering is on the instance of Arrays, and are accessable through the prototype-chain. The correct term would have been Array.prototype.map You've even linked to the mdn page were it is correctly defined, don't just remove things you don't understand - when you are trying to teach others. Don't forget to make proper research. More on reddit.com
🌐 r/reactjs
9
1
July 14, 2021
reactjs - Object.entries.map to render react components array - Stack Overflow
From your question, it seems like ... as object in react state. And it depends on your requirement. store in array, if data is used only for display purpose and not going to be changed using any event. like, to render dynamic options in select box. You can store values as array and can use 'Array.map' to create ... More on stackoverflow.com
🌐 stackoverflow.com
How to render element in React.js from object map
I have a question about rendering components from object map. I have a structure like this: const tabs = [ 'tab1': { name: 'Tab 1', renderComponent: () => }, 'tab2': { name: 'Tab 2', renderComponent: () => } ]; I want to access the renderComponent property and render sub component. Tried with function invocation, React... More on stackoverflow.com
🌐 stackoverflow.com
Rendering an array.map() in React - javascript
I am having a problem where I am trying to use array of data to render a element. In the code below the console logs are working fine, but the list items aren't appearing. var Main = React. More on stackoverflow.com
🌐 stackoverflow.com
People also ask

How to use map object in React?
The **map object** can be used in React to iterate over arrays and render components dynamically. You can utilize the map method to return a new array of components based on your data. For instance, if you have an array of user objects, you can create a list of User components like this: ```javascript const users = [{ id: 1, name: 'Alice' }, { id: 2, name: 'Bob' }]; const UserList = () => { return (
    {users.map(user => (
  • {user.name}
  • ))}
); }; ``` In this **above example**, each user is rendered as a list item, demonstrating how map can simplify rendering logic in React.
🌐
dhiwise.com
dhiwise.com › blog › design-converter › how-to-use-react-object-map-for-clean-code
React Object Map: Best Practices For Developers
How do you map a list of objects in React?
You can map a list of objects in React by using the map method on the array of objects, returning a React component for each object. Here’s an example where we have an array of product objects: ```javascript const products = [ { id: 1, name: 'Laptop', price: 999 }, { id: 2, name: 'Phone', price: 499 }, ]; const ProductList = () => { return ( {products.map(product => (

{product.name}

Price: ${product.price}

))} ); }; ``` In this **above example**, each product is rendered with its name and price, showcasing how to effectively map through a list of objects in React.
🌐
dhiwise.com
dhiwise.com › blog › design-converter › how-to-use-react-object-map-for-clean-code
React Object Map: Best Practices For Developers
What is an object map?
An **object map** is a data structure that allows you to store key-value pairs, where each key is unique. This structure enables efficient access to the corresponding value based on the key. In JavaScript, objects serve as maps, allowing you to create mappings like this: ```javascript const map = { 'key1': 'value1', 'key2': 'value2', }; console.log(map['key1']); // Outputs: value1 ``` Using an **object map** can enhance data retrieval efficiency, especially when dealing with large datasets.
🌐
dhiwise.com
dhiwise.com › blog › design-converter › how-to-use-react-object-map-for-clean-code
React Object Map: Best Practices For Developers
🌐
Stack Overflow
stackoverflow.com › questions › 56008650 › render-react-component-from-map-object
Render react Component from Map Object - javascript
In reactjs I'm trying to render a component from a Map object. More specifically, on the press of a button I create a new "FormRow" component, and I store it in a javascript Map object for convenie...
🌐
Atomizedobjects
atomizedobjects.com › blog › react › how-to-render-an-array-of-objects-with-map-in-react
How to render an array of objects with Array.map in React | Atomized Objects
If you want to learn more about ... an array of objects in react with JSX we need to use Array.map() to transform the object into something react can make use of because you cannot directly render an object into React....
🌐
DhiWise
dhiwise.com › blog › design-converter › how-to-use-react-object-map-for-clean-code
React Object Map: Best Practices For Developers
January 17, 2025 - When working with React, you often encounter situations where you need to map over objects to render components dynamically. React’s declarative nature makes it easy to create reusable components, and understanding how to implement object mapping can enhance your application's efficiency.
🌐
DEV Community
dev.to › mazin1231 › map-method-in-react-js-4i2o
Map() Method In React JS. - DEV Community
November 7, 2022 - To render an array of objects in react with JSX we need to use Array.map() to transform the object into something react can make use of because you cannot directly render an object into React.
Find elsewhere
🌐
Linguine Code
linguinecode.com › home › blog › how to use array.map() to render data in react
How to use Array.map() to render data in React
December 18, 2020 - Here’s an example how to use Array.map() in React. <ul> {people.map(person => { return ( <li key={person.id}> {person.name} - {person.age} years old </li> ) })} </ul> Let’s breakdown this code. In the JSX section you’re going to start by adding curly braces ({}). ... The curly braces ({}) are a special syntax to let the JSX parser know that it needs to interpret the part as JavaScript. In the example above, I want to execute an Array.map() to render the people data.
🌐
Hackingwithreact
hackingwithreact.com › read › 1 › 13 › rendering-an-array-of-data-with-map-and-jsx
Rendering an Array of Data with map() and JSX – a free Hacking with React tutorial
So when we delete Dave, React could see that numbers 1 and 3 remained and update accordingly. Back to our Detail component with its random names and places: we can provide a key by using the index value we are receiving from map(), like this: ... render() { return (<div> {this.state.people.map((person, index) => ( <p key={index}>Hello, {person.name} from {person.country}!</p> ))} </div>); }
🌐
Scrimba
scrimba.com › articles › react-list-array-with-map-function
How to use Array.map to render a list of items in React
November 1, 2022 - React uses a virtual DOM to keep track of any changes that are made and keys are important in making sure that these changes are as rendered in the most efficient way possible. An easy way to create a key prop is to use the index of the array: const Headings = () => { const headings = heroes.map((hero,index) => <h1 key={index}>{hero}</h1>) return <header>{headings}</header> }
🌐
React
legacy.reactjs.org › docs › lists-and-keys.html
Lists and Keys – React
Usually you would render lists inside a component. We can refactor the previous example into a component that accepts an array of numbers and outputs a list of elements. function NumberList(props) { const numbers = props.numbers; const listItems = numbers.map((number) => <li>{number}</li> ); return ( <ul>{listItems}</ul> ); } const numbers = [1, 2, 3, 4, 5]; const root = ReactDOM.createRoot(document.getElementById('root')); root.render(<NumberList numbers={numbers} />);
🌐
freeCodeCamp
freecodecamp.org › news › destructure-object-properties-using-array-map-in-react
How to Destructure Object Properties Using array.map() in React
November 2, 2022 - Say you have a list of items in an array that needs to be rendered as a React component onto a web page. The ideal way to map a series of items in an array looks like this: const shoppingList = ['Oranges', 'Cassava', 'Garri', 'Ewa', 'Dodo', 'Books'] export default function List() { return ( <> {shoppingList.map((item, index) => { return ( <ol> <li key={index}>{item}</li> </ol> ) })} </> ) } The snippet above pretty much fulfills its purpose. But what if you have to map through an array of objects with multiple properties?
Top answer
1 of 2
5

From your question, it seems like you are confused when to store values in array and values as object in react state. And it depends on your requirement.

  • store in array, if data is used only for display purpose and not going to be changed using any event. like, to render dynamic options in select box. You can store values as array and can use 'Array.map' to create component array and render it.

  • Objects are generally used when you are changing data / update data on events. Objects are handy when it comes to update specific value inside it. like, you are rendering table row with checkbox in first column and on checkbox mark/unmark you need to update related row data property.

If your application requires both things to be achieved, you should do as following,

{
    areas: ['areaid_0', 'areaid_1', 'areaid_2'],
    areaDetails: {
        'areaid_0': {title: 'Some title', isSelected: false},
        'areaid_1': {title: 'Some title', isSelected: false},
        'areaid_2': {title: 'Some title', isSelected: false}
    }
}

When it comes to rendering you can do like,

this.state.areas.map(x => {
  const areaDetail = this.state.areaDetails[x];
  <Component key={x} {...areaDetail}... />
})

When you wants to update any object, you can do like,

this.state.areaDetails[x].isSelected = true;

Hope, this will be helpful to you.

2 of 2
5

Storing objects in a sort of "ID map" like this is something that is often recommended by the Redux docs. Note, the keys are the IDs by convention, not an arbitrary attribute like title.

While you can indeed use the Object.entries style that you have demonstrated, you are almost always going to want to have some sort of logical sort order.

You should not rely on JavaScript retaining sort order of keys, and for this reason, libraries like normalizr (which Abramov recommends) that automatically convert to this normalized structure will also return a result array of IDs that will retain sort order. Reference Here

The "Normalizing State Shape" docs from Redux also explicitly recommend you rely on an array of IDs for sort order:

  • Any references to individual items should be done by storing the item's ID.
  • Arrays of IDs should be used to indicate ordering.

Your state shape might look like this:

{
  areas: {1: {id: 1, title: 'some title'}, 2: {id: 2, title: 'other title'}},
  areaIds: [1, 2],
}

You would then just iterate like this:

areaIds.map(id => <Component key={id} area={areas[id]} />)

If you truly want to do the Object.entries way, you can use a library like lodash which lets you do simply:

_.map(areas, area => <Component key={area.id} area={area} />)
🌐
Pluralsight
pluralsight.com › blog › tech guides & tutorials
Map JavaScript Object Keys Using React | Pluralsight
September 16, 2020 - What if you want to dynamically display a table in your React view using each object's keys as the column headers? This solution requires that you map over an object's keys. ES6 JavaScript provides a built-in method for this—Object.keys. This method takes in an object and returns an array of keys. Here is an example of how you could populate a simple table using this built-in method: render() { const firstDog = Array.isArray(this.dogs) && this.dogs.length ?
🌐
DEV Community
dev.to › kellduran › the-map-method-to-render-data-in-react-od2
The Map Method to Render Data in React - DEV Community
July 31, 2022 - It creates a new array which is populated by executing whatever actions the callback function requests on each of the elements in calling array. The MDN documentation can be found here:(https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map) Here is an example of rendering data with Array.map() in React in the same functional component.
🌐
freeCodeCamp
freecodecamp.org › news › how-to-render-lists-in-react
How to Render Lists in React using array.map()
April 10, 2023 - Now, you need to return JSX that renders every applicant name as presented from the array. To get the names of the applicants, you can easily do that with JavaScript's array.map method. Below is how you can map every applicant's name: import React from 'react'; const applicants = [ { name: 'Joe', work: 'freelance-developer', blogs: '54', websites: '32', hackathons: '6', location: 'morocco', id: '0', }, { name: 'janet', work: 'fullstack-developer', blogs: '34', websites: '12', hackathons: '8', location: 'Mozambique', id: '1', }, ]; function App() { return ( <> {applicants.map(function(data) { return ( <div> Applicant name: {data.name} </div> ) })} </> ) } export default App;
🌐
Dykraf
dykraf.com › home › web development and seo blog › how to use map function in javascript for rendering a react component
How To Use Map Function In JavaScript For Rendering A React Component
January 2, 2023 - In React, you can use the map() method to create a list of elements from an array of data to render a list of elements in a React component Web Story · In JavaScript, the map() method is used to apply a function to each element in an array, ...