In my project I need to display data from 2 separate arrays in one table and I need to make my code responsive, my code:
import "./styles.css";
export default function App() {
const data = [
{
v: "car"
},
{
v: "model"
},
{
v: "accident"
},
{
v: "owner"
}
];
const info = [
{
year: "2010",
model: "bmv",
accident: "2019",
owner: "J.S"
},
{
year: "2012",
model: "bmv",
accident: "n/a",
owner: "D.W" },
{
year: "2014",
model: "bmv",
accident: "2016",
owner: "J.B"
}
];
return (
<div className="App">
<table>
<tr>
{data?.map((d, i) => (
<td>{d.v}</td>
))}
{info.map((d) => (
<>
<td>{d.year}</td>
<td>{d.model}</td>
<td>{d.accident}</td>
<td>{d.owner}</td>
</>
))}
</tr>
</table>
</div>
);
}
I'm mapping through both arrays, result is displayed like this:
car model accident owner 2010 bmv 2019 J.S 2012 bmv n/a D.W 2014 bmv 2016 J.B
but I need the result to be:
car 2010 2012 2014
model bmv bmv bmv
accident 2019 n/a 2016
owner J.S D.W J.B
another problem is that I don't want to specify {d.year} or {d.model in
{info.map((d) => (
<td>{d.year}</td>
<td>{d.model}</td>
<td>{d.accident}</td>
<td>{d.owner}</td>
))}
because when/if new value is added (like lastOwner) it won't be displayed unless I specify {d.lastOwner}
Is there any way to actually combine data from 2 arrays and just map through all the values without specifying them? Any advice and help are greatly appreciated.