Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to get duration from array of objects with time stamps in typescript

I have one array of objects, these objects have a timestamp attribute, I want to calculate the duration from the time stamp:

myArray = [obj1, obj2, obj3];
ob1 = {"attr1":val1, "attr2":val2, "timestamp": ...};

I want to get the duration between consecutive objects: (in python it would look a bit like this:
durations = myArray[1:]-myArray[:-1])

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can slice the array to get a copy without the first element, then apply map over it.

let durationDiffs = myArray.slice(1).map((x, i) => x.timestamp - myArray[i].timestamp);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading