What you need is to map your array of objects and remember that every item will be an object, so that you will use for instance dot notation to take the values of the object.
In your component
[
{
name: 'Sam',
email: 'somewhere@gmail.com'
},
{
name: 'Ash',
email: 'something@gmail.com'
}
].map((anObjectMapped, index) => {
return (
<p key={`${anObjectMapped.name}_{anObjectMapped.email}`}>
{anObjectMapped.name} - {anObjectMapped.email}
</p>
);
})
And remember when you put an array of jsx it has a different meaning and you can not just put object in your render method as you can put an array.
Take a look at my answer at mapping an array to jsx
Answer from FurkanO on Stack OverflowWhat you need is to map your array of objects and remember that every item will be an object, so that you will use for instance dot notation to take the values of the object.
In your component
[
{
name: 'Sam',
email: 'somewhere@gmail.com'
},
{
name: 'Ash',
email: 'something@gmail.com'
}
].map((anObjectMapped, index) => {
return (
<p key={`${anObjectMapped.name}_{anObjectMapped.email}`}>
{anObjectMapped.name} - {anObjectMapped.email}
</p>
);
})
And remember when you put an array of jsx it has a different meaning and you can not just put object in your render method as you can put an array.
Take a look at my answer at mapping an array to jsx
@FurkanO has provided the right approach. Though to go for a more cleaner approach (es6 way) you can do something like this
[{
name: 'Sam',
email: 'somewhere@gmail.com'
},
{
name: 'Ash',
email: 'something@gmail.com'
}
].map( ( {name, email} ) => {
return <p key={email}>{name} - {email}</p>
})
Cheers!
How to render an array of objects with Array.map in React
How to map over 2 Array of object in React and render to Table
Map Over An Array of Objects in React
React - Use Array.map() to Dynamically Render Elements - .map is different than in js
How to render an array of objects in React?
Step 1: Create a react application.
Step 2: Change directory.
Step 3: Create data as an array.
Step 4: Mapping the array into a new array of JSX nodes as arrayDataItems.
Step 5: Return arrayDataItems from the component wrapped in
How do you set an array of objects in state in React JS?
To set an array of objects in the state of a React component, you can use the 'useState' hook. So to do this, first, import 'useState' from 'react'. Then, declare a state variable using useState and initialize it with your array of objects. To update the state, use the setter function provided by the useState hook. And voila, now you can easily manage and modify the array of objects within your component's state.
How do you iterate an array of objects in React JS?
To iterate through an array of objects in ReactJS, you must use the map () method. It creates a new array by applying a provided function to each element of the original array. Within the function, you can access and render each object's properties as and when needed, effectively iterating through and rendering them in your React component.
Hi Everyone,
I have just posted a new article about rendering objects and data in react with Array.prototype.map for beginners.
How to render an array of objects with Array.map in React
Please have a read and let me know about any constructive feedback you might have in order for me to keep improving it.
Thanks!
how can i map over 2 Array of object in React and render to one Table
const [dataSetOne, setDataSetOne] = useState()
const [dataSettwo, setDataSetTwo] = useState()
``let URL1 = "http://api_url/users"
let URL2 = "http://api_url/users-card"
const promise1 = axios.post(URL1, inputValue , {headers: {'Content-Type': 'text/plain'}});
const promise2 = axios.post(URL2, inputValue , {headers: {'Content-Type': 'text/plain'}});
Promise.all([promise1, promise2]).then(function(values) {
setDataSetOne(values[0]);
setDataSetTwo(values[1]);
}); <TableContainer>
<Table>
<TableHead>
<TableRow>
<TableCell>Dessert (100g serving)</TableCell>
<TableCell align="right">Calories</TableCell>
<TableCell align="right">Fat (g)</TableCell>
</TableRow>
</TableHead>
<TableBody>
<TableCell>{DataOne}</TableCell>
<TableCell>{DataOne}</TableCell>
<TableCell>{DataTwo}</TableCell>
</TableBody>
</Table>
</TableContainer>