使用 dwl 2.0 展平具有可变深度的嵌套数组
flattening of nested arrays with variable depth with dwl 2.0
我有以下数据:
{
"currencyCode": "GBP",
"type": "New",
"orderLines": [
{
"currencyCode": "EUR",
"id": "8023O000000Cc8pQAC",
"orderLines": [
{
"currencyCode": "EUR",
"id": "8023O000000Cc8qQAC",
"parentId": "8023O000000Cc8pQAC",
"orderLines": []
},
{
"currencyCode": "EUR",
"id": "8023O000000Cc8rQAC",
"parentId": "8023O000000Cc8pQAC",
"orderLines": []
}
]
},
{
"currencyCode": "EUR",
"id": "8023O000000Cc8vQAC",
"orderLines": [
{
"currencyCode": "EUR",
"id": "8023O000000Cc8wQAC",
"parentId": "8023O000000Cc8vQAC",
"orderLines": []
}
]
}
]
}
注意:orderLines 数组可以是任意深度
我想要这样的输出:
{
"currencyCode": "GBP",
"type": "New",
"orderLines": [
{
"currencyCode": "EUR",
"id": "8023O000000Cc8pQAC",
"orderLines": []
},
{
"currencyCode": "EUR",
"id": "8023O000000Cc8qQAC",
"parentId": "8023O000000Cc8pQAC",
"orderLines": []
},
{
"currencyCode": "EUR",
"id": "8023O000000Cc8rQAC",
"parentId": "8023O000000Cc8pQAC",
"orderLines": []
},
{
"currencyCode": "EUR",
"id": "8023O000000Cc8vQAC",
"orderLines": []
},
{
"currencyCode": "EUR",
"id": "8023O000000Cc8wQAC",
"parentId": "8023O000000Cc8vQAC",
"orderLines": []
}
]
}
您可以使用执行以下操作的递归函数来完成此操作:
- 如果关卡中没有元素returns
[]
- 递归地获取每个级别的
orderLines
(没有子字段)
- 追加下一级的
orderLines
像这样:
%dw 2.0
output application/json
fun getNextLevel(orders: Array) = flatten(orders.orderLines)
fun removeChildren(orders: Array) =
orders map ((value) ->
value - "orderLines"
)
fun getOrders(orders: Array) = do {
orders match {
case [] -> []
else -> removeChildren(orders) ++ getOrders(getNextLevel(orders))
}
}
---
{
"currencyCode": payload.currencyCode,
"type": payload."type",
"orderLines": getOrders(payload.orderLines)
}
我有以下数据:
{
"currencyCode": "GBP",
"type": "New",
"orderLines": [
{
"currencyCode": "EUR",
"id": "8023O000000Cc8pQAC",
"orderLines": [
{
"currencyCode": "EUR",
"id": "8023O000000Cc8qQAC",
"parentId": "8023O000000Cc8pQAC",
"orderLines": []
},
{
"currencyCode": "EUR",
"id": "8023O000000Cc8rQAC",
"parentId": "8023O000000Cc8pQAC",
"orderLines": []
}
]
},
{
"currencyCode": "EUR",
"id": "8023O000000Cc8vQAC",
"orderLines": [
{
"currencyCode": "EUR",
"id": "8023O000000Cc8wQAC",
"parentId": "8023O000000Cc8vQAC",
"orderLines": []
}
]
}
]
}
注意:orderLines 数组可以是任意深度
我想要这样的输出:
{
"currencyCode": "GBP",
"type": "New",
"orderLines": [
{
"currencyCode": "EUR",
"id": "8023O000000Cc8pQAC",
"orderLines": []
},
{
"currencyCode": "EUR",
"id": "8023O000000Cc8qQAC",
"parentId": "8023O000000Cc8pQAC",
"orderLines": []
},
{
"currencyCode": "EUR",
"id": "8023O000000Cc8rQAC",
"parentId": "8023O000000Cc8pQAC",
"orderLines": []
},
{
"currencyCode": "EUR",
"id": "8023O000000Cc8vQAC",
"orderLines": []
},
{
"currencyCode": "EUR",
"id": "8023O000000Cc8wQAC",
"parentId": "8023O000000Cc8vQAC",
"orderLines": []
}
]
}
您可以使用执行以下操作的递归函数来完成此操作:
- 如果关卡中没有元素returns
[]
- 递归地获取每个级别的
orderLines
(没有子字段) - 追加下一级的
orderLines
像这样:
%dw 2.0
output application/json
fun getNextLevel(orders: Array) = flatten(orders.orderLines)
fun removeChildren(orders: Array) =
orders map ((value) ->
value - "orderLines"
)
fun getOrders(orders: Array) = do {
orders match {
case [] -> []
else -> removeChildren(orders) ++ getOrders(getNextLevel(orders))
}
}
---
{
"currencyCode": payload.currencyCode,
"type": payload."type",
"orderLines": getOrders(payload.orderLines)
}