Skip to content
Advertisement

Compare array values and selecting specific one

I need a bit of help with the below concept.

I have an array:

JavaScript

I would like to loop through it and select the “first IN” and “last OUT” based on “dateTime”. So I should be getting:

JavaScript

How is it possible to achieve this? Appreciate any help.

Thanks,

Advertisement

Answer

The way I would approach this is to:

  1. Filter the RAW input array to the earliest in and the latest out. We don’t need the rest of the results.

  2. Re-format that output in the format expected.

The dateTime strings appear to be properly formatted time stamps (presumably from a mysqli database). Which is good because it means we can compare the timestamps with < and >.

Solution

JavaScript

Output

JavaScript

Code without comments

JavaScript
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement