I'm quite new to both react and JavaScript and would really appreciate some advice on this. I want to iterate through a nested array like the one below by using the map() method:
const demos = [
{
id : '1',
name: 'name1',
dates: ['jan', 'feb', 'apr']
},
{
id : '2',
name: 'name2',
dates: ['feb', 'may']
}
];
I want to first map through the first object with the first date and then the second date and so on, before going to the next object and then sort it based on date to ideally achieve the following output:
name1: jan
name1: feb
name2: feb
name1: apr
name2: may
The only not quite working solution I've come up with this far is:
import * as React from 'react';
const demos = [
{id : '1',
name: 'name1',
dates: ['jan', 'feb', 'apr']
},
{id : '2',
name: 'name2',
dates: ['feb', 'may']
}
];
const counts = ['0','1','2'];
export default function Test() {
return (
<div>
{
counts.map(count => (
demos.map(demo =>(
<div key={demo.id}>
<div>{demo.name}: {demo.dates[count]}</div>
</div>
))
))
}
</div>
);
}
which gives me the following output:
name1: jan
name2: feb
name1: feb
name2: may
name1: apr
name2:
using the const counts isn't really working and doesn't feel like the proper way to begin with. How can I do this in a good way and then sort it by date ?