How to find second unique date using JS, e.g. output should return 2019-12-25T02:34:00Z
const data = [
{ date: '2019-12-24T03:24:00Z' },
{ date: '2019-12-24T04:56:00Z' },
{ date: '2019-12-25T02:34:00Z' },
{ date: '2019-12-26T01:23:00Z' }
]
How to find second unique date using JS, e.g. output should return 2019-12-25T02:34:00Z
const data = [
{ date: '2019-12-24T03:24:00Z' },
{ date: '2019-12-24T04:56:00Z' },
{ date: '2019-12-25T02:34:00Z' },
{ date: '2019-12-26T01:23:00Z' }
]
2
Answers
Collect dates with counts and find the first date with count === 1 (unique):
You could reduce (right) all the dates and build a lookup may by unique date-only. Reducing right will keep the first value as it appears in the original array, but will reverse the output, so you should reverse at the very end.
Output:
Here is a version without reversing. It requires a convenience function to only update the lookup map, if the key does not already exist.