Get MIN (oldest) or MAX (most recent) date from array of dates
Hi,
I first do an SQL Server - Execute and get this weird {_snaptype_localdatetime: "..."} for the CreationDate and LastUpdate fields. Then, using a Mapper snap I do toString() to just get the date fields in string form.
Then, I do a Group By on some fields and add another Mapper snap where I have this:
Here, I am trying to get the MAX and MIN dates from the array.
This is what the whole pipeline looks like:
How can I get the oldest and the most recent dates from the array?
Thank you very much in advance!
Kind regards,
Adam
adam_gataev - since you have it in an array, you can use the Array.reduce() method to get the min or max value out of the array. It would look like this:
$dateArray.reduce ((accum,cur)=> accum == null || accum > cur ? cur : accum, null)
Note that this also uses the ternary operator for the if/then/else condition to determine which value is greater when comparing the array elements. One nice feature of this method is that it doesn't matter what the data type of the values are, as long as they are comparable.
There are other ways you could do this, but this was my initial thought. If you're interested, I've attached an expression library that I created a while back and share with many of my clients as it has many common functions that have been requested. To use the expr lib, just include it into the pipeline properties, then your expressions to get the min/max values just use the string.min() and string.max() methods. Note the use of the spread operator to expand the array to individual values since the expr lib method is expecting a list of values rather than an array.
Hope this helps!