Answer the question
In order to leave comments, you need to log in
How to make a map in React?
I want to display a list of subjects (projects) grouped by month and year...
For grouping, I did the following and printed the results, which seem to be good:
// this gives an object with dates as keys
const groups = filteredProjects.reduce((groups, project) => {
//const date = project.Properties.ModificationTime.Value.split('T')[0];
//const month = moment(project.Properties.ModificationTime.Value, 'YYYY-MM-DD').format('MMM');
const date = moment(project.Properties.ModificationTime.Value, 'YYYY-MM-DD').format('MMM-YYYY');
if (!groups[date]) {
groups[date] = [];
}
groups[date].push(project);
return groups;
}, {});
// Edit: to add it in the array format instead
const groupedProjects = Object.keys(groups).map((date) => {
return {
date,
projects: groups[date]
};
});
{groupedProjects.map((date, arr) => (
<div className="inline">
{date}
{arr.map((item) =>
<Project
key={item.Name}
project={item}
/>
)}
</div>
))}
Answer the question
In order to leave comments, you need to log in
doesn't work because
const groupedProjects = Object.keys(groups).map((date) => {
return {
date,
projects: groups[date]
};
});
groupedProjects.map((date, arr) => (
<div className="inline">
{date}
{arr.map((item) =>
<Project
key={item.Name}
project={item}
/>
)}
</div>
))}
groupedProjects.map(({ date, projects }) => (
{date}
{projects.map()}
)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question