I have a nested array in html table. Am able to display the table. However, i have a problem with inserting headers to the nested arrays columns.
Below is the code:
table.js
const Table= college?.map((item =>
<tr >
<td> {item.department.map((item,i) => (
<tr>
<td>{item.studentName}</td>
<td>{item.subject}</td>
<td>{item.daysClassattended}</td>
</tr>
))}
</td>
</tr>
));
return (
<div>
<table>
<thead>
<tr>
<th>Student Name </th>
<th>Subject </th>
<th>Days Attended Classes</th>
</tr>
</thead>
<tbody>{Table}</tbody>
</table>;
</div>
What is the best way to insert headers into the nested array table? Thanks in advance
2
Answers
If you’re just looking to list all the students together without distinguishing their departments, then
flatMap
is an option:To insert headers to the nested array columns in the HTML table, you can modify the code as follows: