Dataweave 2.0 maxBy and filter

ⅰ亾dé卋堺 提交于 2021-02-10 14:19:40

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!