问题
I'd like to filter an array by a maximum value, and have it return all elements with that value. Dataweave is just returning 1.
This is dataweave 2.0 on Mule 4. First, there should be a groupBy to group elements with the same id. Then, within each element group, there should be a search for the largest check_date, and only those within each element group with that date should be returned.
%dw 2.0
output application/json
var arr = [
{
"id": "001P000001MPf0XIAT",
"code": "580",
"type": "M",
"check_date": "2017-09-08T12:00:00"
},
{
"id": "001P000001MjaVOIAZ",
"code": "ZN1",
"type": "A",
"check_date": "2018-05-01T12:00:00"
},
{
"id": "001P000001MjaVOIAZ",
"code": "ZN1",
"type": "M",
"check_date": "2018-05-01T12:00:00"
},
{
"id": "001P000001MjaVOIAZ",
"code": "ZN1",
"type": "A",
"check_date": "2017-11-01T12:00:00"
},
{
"id": "001P000001MPf0XIAT",
"code": "580",
"type": "M",
"check_date": "2019-04-12T12:00:00"
}
]
---
arr groupBy $.id mapObject (value,key) -> {
grouping:{
id:key,
rows:value maxBy $.check_date
}
}
回答1:
rows: value filter ((item, index) ->
item.check_date == (value maxBy $.check_date).check_date)
来源:https://stackoverflow.com/questions/57279351/dataweave-2-0-maxby-and-filter