Assuming that you have an array of events and each event has a start property, you can refactor the code as follows to achieve the same result:
index.tsx265 chars5 lines
Explanation:
sort
method sorts the events array by start date using a compare function that compares a.start
and b.start
.map
method maps each event date to a string representation of the date using toISOString
and split
methods.filter
method filters out duplicates in the array by comparing the current value with the first index of the value using the indexOf
method.slice
method returns the first 20 elements of the resulting array.gistlibby LogSnag