HabitatMap/AirCasting

View on GitHub
app/javascript/react/utils/lastArrayItem.ts

Summary

Maintainability
A
0 mins
Test Coverage
interface DateValuePair {
  date: string;
  value: number;
}

const lastItemFromArray = (
  array: DateValuePair[]
): DateValuePair | undefined => {
  if (array.length === 0) return undefined;

  const sortedArray = [...array].sort(
    (a, b) => new Date(a.date).getTime() - new Date(b.date).getTime()
  );
  return sortedArray[sortedArray.length - 1];
};

export { lastItemFromArray };