0

我有看起来像这样的 csv 有效负载

id,header,value
1,addr1,hyd
1,addr2,blr
2,addr1,rpr
2,addr2,knk
2,addr3,jpn

要求是将输出有效载荷转换为此

id,addr1,addr2,addr3
1,hyd,blr,
2,rpr,knk,jpn

我尝试了几种涉及 groupBy 的方法,但无法实现所需的输出。这就是我的 groupBy 的样子payload groupBy (item) -> item.id

有人可以帮我吗?提前致谢。

4

1 回答 1

2
%dw 2.0
output application/csv
// Get the distinct headers, maybe you want them ordered too
var headers = "id" >> (payload.*header distinctBy $ orderBy $)
// Get the rows but organize them so that you have objects {header: value}
var rows = payload groupBy $.id 
mapObject (
    {($$): 
        $ reduce (e, acc={id: $$}) -> acc ++ {(e.header): e.value}
    }
) 
pluck $
---
// Iterate over the rows
rows map (
    // Iterate over the headers and add the values
    headers reduce (e, acc={}) -> acc ++ {(e): $[e]}
)
于 2019-10-10T09:30:18.210 回答