数据编织脚本中的Maps变量查找列表 [英] Lookup list of Maps variable in data weave script
问题描述
我有一个地图列表(listOfMapsObject),如下所示
I have a list of maps (listOfMapsObject) like below
[
{
"Id" : "1234",
"Value" : "Text1"
},
{
"Id" : "1235",
"Value" : "Text2"
}
]
我想在dataweave脚本中访问给定ID的值"字段. 例如:对于Id = 1234,应返回Text1.
I would like access "Value" field for a given Id in dataweave script. For example: For Id = 1234, Text1 should be returned.
%dw 1.0
%output application/json
%var listOfMapsObject = flowVars.listOfMaps
---
payload map {
"key" : $.key,
"data" : lookup Value field in listOfMapsObject by given key
}
推荐答案
@'sulthony h'建议的方法很好,但是如果您在pyload和listOfMapsObject中有大量数据,它将最终导致性能问题.使用filter时,对于有效负载脚本的每个记录,都会为flowVars.listOfMaps中的所有条目循环.
Approch suggested by @'sulthony h' is fine but it will end up in performance issue if you large number of data in pyload and listOfMapsObject. As filter is used , for each record of payload script will loop for all the entries in flowVars.listOfMaps.
以下操作将正常进行,并且仅映射一次键值.
Following will work fine and map key value only once.
%dw 1.0
%output application/json
%var dataLookup = {(flowVars.listOfMaps map {
($.Id): $.Value
})}
---
payload map {
key : $.key,
data : dataLookup[$.key]
}
输出-
[
{
"key": "1234",
"data": "Text1"
},
{
"key": "1235",
"data": "Text2"
}
]
有效负载-
[
{
"key" : "1234"
},
{
"key" : "1235"
}
]
然后-
[
{
"Id" : "1234",
"Value" : "Text1"
},
{
"Id" : "1235",
"Value" : "Text2"
}
]
希望这会有所帮助.
这篇关于数据编织脚本中的Maps变量查找列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!