Answer the question
In order to leave comments, you need to log in
How to properly display an array of objects that can contain an array?
I have arrays like this:
const qualities = {
tedious: { _id: "67rdca3eeb7f6fgeed471198", name: "Нудила", color: "primary" },
strange: { _id: "67rdca3eeb7f6fgeed471100", name: "Странный", color: "secondary" },
buller: { _id: "67rdca3eeb7f6fgeed4711012", name: "Троль", color: "success" },
alcoholic: { _id: "67rdca3eeb7f6fgeed471101", name: "Алкоголик", color: "danger" },
handsome: { _id: "67rdca3eeb7f6fgeed471102", name: "Красавчик", color: "info" },
uncertain: { _id: "67rdca3eeb7f6fgeed471103", name: "Неуверенный", color: "dark" },
}
const users = [
{
_id: "67rdca3eeb7f6fgeed471815",
name: "Джон Дориан",
profession: professions.doctor,
qualities: [qualities.tedious, qualities.uncertain, qualities.strange],
completedMeetings: 36,
rate: 2.5
},
{
_id: "67rdca3eeb7f6fgeed471816",
name: "Кокс",
profession: professions.doctor,
qualities: [qualities.buller, qualities.handsome, qualities.alcoholic],
completedMeetings: 15,
rate: 2.5
}
let res = users.map((item) => {
return <tr key={item._id}> <td>{item.name}</td><td>{item.profession}</td><td>{item.qualities.name}</td><td>{item.completedMeetings}</td><td>{item.rate}</td></tr>
})
console.log('res', res)
return <>
<table className="table-danger">
<thead>
<tr>
<th scope="col">First</th>
<th scope="col">Last</th>
<th scope="col">Handle</th>
<th scope="col">Last</th>
<th scope="col">Handle</th>
</tr>
</thead>
<tbody>
{res}
</tbody>
</table>
</>
Answer the question
In order to leave comments, you need to log in
Instead of item.qualities.name you need this:
item.qualities.map(q => q.name).join(', ')
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question