需要一种有效的解决方案,用阵列2中的值替换阵列1(主列表)中的值。当前代码在Dataweave中工作。但是,当两个列表包含更多值时,我认为这不是最佳解决方案。建议使用从数组2中的值更新数组1的最佳解决方案。我正在使用Mule 3.8.5。我想要一个解决方案,以便只需要在第二个列表中进行一次迭代,并替换主列表中的所有值。当前,样本中两次使用了过滤条件。但是在实际情况下,我需要替换8个这样的值。
%dw 1.0
%output application/java
%var arr1 =[
{
"leadId": 127,
"playerId": 334353,
"name": "Joseph",
"activity": "10/03/2015"
},
{
"leadId": 128,
"playerId": 334354,
"name": "Sam",
"activity": "10/03/2017"
},
{
"leadId": 124,
"playerId": "",
"name": "",
"activity": "10/03/2015"
},
{
"leadId": 123,
"playerId": "",
"name": "",
"activity": "10/03/2015"
}
]
%var arr2 = [
{
"playerId": 123456,
"name": "James",
"leadId": 124
},
{
"playerId": 7890,
"name": "Jacob",
"leadId": 123
}
]
---
arr1 map ((actData) -> {
"leadId":actData.leadId,
"playerId": (arr2 filter ($.leadId == actData.leadId))[0].playerId default actData.playerId,
"name": (arr2 filter ($.leadId == actData.leadId))[0].name default actData.name,
"activity": actData.activity
})
上面的代码给出了预期的结果。但是当列表值更多时,它非常慢。
答案 0 :(得分:1)
嗨,您应该使用groupBy,以便按条件将其编入索引,然后每次都进行查找而不是过滤器
%dw 1.0
%output application/json
%var arr1 =[
{
"leadId": 127,
"playerId": 334353,
"name": "Joseph",
"activity": "10/03/2015"
},
{
"leadId": 128,
"playerId": 334354,
"name": "Sam",
"activity": "10/03/2017"
},
{
"leadId": 124,
"playerId": "",
"name": "",
"activity": "10/03/2015"
},
{
"leadId": 123,
"playerId": "",
"name": "",
"activity": "10/03/2015"
}
]
%var arr2 = [
{
"playerId": 123456,
"name": "James",
"leadId": 124
},
{
"playerId": 7890,
"name": "Jacob",
"leadId": 123
}
]
%var arr2ByLeadId = arr2 groupBy $.leadId
---
arr1 map ((actData) -> {
"leadId":actData.leadId,
"playerId": arr2ByLeadId."$(actData.leadId)"[0].playerId default actData.playerId,
"name": arr2ByLeadId."$(actData.leadId)"[0].name default actData.name,
"activity": actData.activity
})