简体   繁体   中英

How do i calculate average times in Typescript

I have an object which has the following entry in it:

   numberOfReturns: number = 0;
   returns_explanations: string [] = [];
   departure_time: string = '';
   arrival_time: string = '';

So in departure_time i have the following information:

...
departure_time: 08:15
departure_time: 08:02
departure_time: 09:00
...

I have an array of this whole object which will need to be iterated and i need to get the average departure_times.

let average: Date = null;

for (let i = 0; i < workAccountItem.length; i++) {
    average = moment(average).add(moment('01/01/2021 ' + workAccountItem[i].departure_time));
}

average = average / workAccountItem.length;

This approach won't work.

In your main type interface, it looks like you have an extra space between string and [] for returns_explanations -- if you're defining a prop as having a type array of strings, it should be string[] with no space. For your type definition for average , you can also set it to being either Date or null like so, average: Date | null average: Date | null , which will prevent type errors when no date is present yet.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM