I have a list of objects order by date. Each object have the folling structure
export class SegmentDTO {
dateInsert: Date;
dateModified: Date;
id: number;
language: number;
content: string;
}
I want to get the disctincs object based on language property, and take the most recent one of each language (based on dateModified)
What I tried is this:
const listLangOrderByDateASC = this.segments.sort((a, b) => new Date(a.dateInsert).getTime() - new Date(b.dateInsert).getTime());
const result = [...new Map(listSourceLangOrderByDateASC.map(item => [item.language, item])).values()];
With this, I can obtain the most recent object for each language, but I don't know if there is a better approach to do it. I discover that making this takes the final objects of the sorted list, but I don't really know if it's just lucky.
Any help would be aprecciated!