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
How to render/map through an array of objects in React
How to map an array of objects in react?
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>You can simply use map to render multiple components from an array:
this.state = { news: [{}, {}] };
...
const news = this.state.news.map((newsItem) =>
<div key={newsItem.id} className="panel-list">{newsItem.title}</div>
);
Note that I added a key attribute to each div. This is important to give the elements a stable identity as explained here. It needs to be an unique identifier for each news item ("id" is used here as an example).
To render the news variable, you just need to use curly braces. For example, to render it in a div:
<div>{news}</div>
You could also render the list directly, without creating a variable (which is a bit messy, though):
<div>
{this.state.news.map((newsItem) =>
<div key={newsItem.id} className="panel-list">{newsItem.title}</div>
)}
</div>
You can do something like the example below
var news = [{
title: 'first title',
date: 'first news date'
},
{
title: 'second title',
date: 'second news date'
},
{
title: 'third title',
date: 'third news date'
}
]
const panelList = document.getElementById('panel-list')
news.map(item => {
panelList.innerHTML += `<div class="panel"><h2 class="panel-title"> ${item.title}</h2><span>${item.date}</span></div>`
})
<div id="panel-list">
</div>
Except the fact that in React you use JSX and you can make that map dirrectly in the render function. The above pure javascript code would translate into :
<div className="panel-list" >
{this.state.news.map(item => (
<div className="panel" key={item.title}>
<h2 className="panel-title">{item.title}</h2>
<span>{item.date}</span>
</div>
))}
</div>
Also, like it has been mentioned in another comment, don't forget to add key attribute to each element inside the map ( each panel ) so React can identify the items correctly.
Given
[
{ fullName: 'vocab Experiment', shortName: 'vocab', ......},
{ fullName: 'mind Experiment', shortName: 'mind', ......},
{ fullName: 'whichenglish Experiment', shortName: 'whichenglish', ......},
];
Seeing as there can be any amount of objects in the list, I would use an if-statement as follows:
{experiments.map(e => {
if (e.shortName === 'vocab') {
return (<Vocab
id={e.shortName}
title={e.fullName}
duration={e.duration}
post={e.tagline}
img={require('../assets/images/quiz/Vocab.png')}
key={e.shortName}
/>)
} else if (e.shortName === 'mind') {
return <Mind ... />
} else if (e.shortName === 'whichenglish') {
return <WhichEnglish ... />
} else return null; // keep react happy
})}
I think you can use array .find() method for this purpose and get exact object for each experiment based on shortName like:
const vocabProps = experiments.find(x => x.shortName === 'vocab') || {};
const mindProps = experiments.find(x => x.shortName === 'mind') || {};
const whichenglishProps = experiments.find(x => x.shortName === 'whichenglish') || {};
and just use spread syntax on the props like:
<Vocab {...vocabProps} />
<Mind {...mindProps} />
<WhichEnglish {...whichenglishProps} />
Or like this, if you want to pass additional props like img to the component.
<Vocab data={vocabProps} img={require('...')}/>
<Mind data={mindProps} />
<WhichEnglish data={whichenglishProps} />