<ol className={classes.ol}>
{props.data.definitions
.sort((a, b) => a.partOfSpeech.localeCompare(b.partOfSpeech))
.map((definition, index) => {
return (
<>
<h3>{definition.partOfSpeech}</h3>
<li key={index} className={classes.list}>
{definition.definition.charAt(0).toUpperCase() +
definition.definition.slice(1)}{" "}
({definition.partOfSpeech})
</li>
</>
);
})}
</ol>
Above is the code that sorts 'partofSpeech' alphabetically and outputs a part of speech with definition of a word. I'm trying to have part of speech shown only once if part of speech is the same. The current output is shown below.
Is it possible to show part of speech only once at the top and render the list based on that part of speech? I've tried using reduce and comparing the object's partOfSpeech still wasn't able to achieve what I wanted.
What I'm trying to achieve: