I've got an array of objects and I'm looking to retrieve the items with unique IDs while also selecting the earliest date.
For example: [{id:1, date: Jan 12}, {id:2, date: Feb 8}, {id:3, date: Feb 8}]
var array = [{id: 1, date: Jan 12 2021 08:00:00 AM}, {id: 2, date: Feb 8 2021 08:00:00 AM}, {id: 2, date: Mar 2 2021 08:00:00 AM}, {id: 3, date: Feb 8 2021 08:00:00 AM}]
Edit: I forgot to mention that the date format is a string formatted as moment("MM-DD-YYYY");
This is what I have so far:
var withId = array.filter(function (g) { return g.Id != null });
withId = array.filter(function (e) { return moment(e.UtcStart) < moment() && e.DteEndDate != null ? moment(e.DteEndDate) > moment() : true });
withId.sort(function (a, b) { return moment(a.UtcStart) - moment(b.UtcStart) });
var key = 'Id';
var unique = [...new Map(withId.map(x => [x[key], x])).values()];