%dw 2.0
output application/json
---
[ { "a" : 1 }, { "a" : 3 }, { "a" : 2 } ] maxBy ((item) -> item.a)
maxBy
maxBy<T>(@StreamCapable array: Array<T>, criteria: (item: T) -> Comparable): T | Null
Iterates over an array and returns the highest value of
Comparable
elements from it.
The items must be of the same type. maxBy
throws an error if they are not,
and the function returns null
if the array is empty.
Parameters
Name | Description |
---|---|
|
The input array. |
|
Expression for selecting an item from the array, where the item is a |
Example
This example returns the greatest numeric value within objects
(key-value pairs) in an array. Notice that it uses item.a
to select the
value of the object. You can also write the same expression like this, using
an anonymous parameter:
[ { "a" : 1 }, { "a" : 3 }, { "a" : 2 } ] maxBy $.a
Example
This example gets the latest DateTime
, Date
, and Time
from inputs
defined in the variables myDateTime1
and myDateTime2
. It also shows that
the function returns null on an empty array.
Source
%dw 2.0
var myDateTime1 = "2017-10-01T22:57:59-03:00"
var myDateTime2 = "2018-10-01T23:57:59-03:00"
output application/json
---
{
myMaxBy: {
byDateTime: [ myDateTime1, myDateTime2 ] maxBy ((item) -> item),
byDate: [ myDateTime1 as Date, myDateTime2 as Date ] maxBy ((item) -> item),
byTime: [ myDateTime1 as Time, myDateTime2 as Time ] maxBy ((item) -> item),
emptyArray: [] maxBy ((item) -> item)
}
}