比较两个数组,如果两个数组中都存在某些数据,则从一个数组中删除该块

问题描述 投票:0回答:1
我需要比较两个具有不同数据字段的数组。比较基于 ID 号,如果两个数组中的数据匹配,我需要从数组中删除整个块/行。例如,在下面的数组中,我需要比较 OrderId(来自 Array1)和 SystemId(来自 Array2),如果 Id 匹配,则需要从 Array1 中删除整个块。请参阅下面的示例数组和输出:

Array1 = [ { "OrderId": "00111111", "BillingCountry": "GB", "CurrencyIsoCode": "GBP", "PersonEmail": "[email protected]" }, { "OrderId": "00222222", "BillingCountry": "US", "CurrencyIsoCode": "USD", "PersonEmail": "[email protected]" } ] Array2 = [ { "SystemId": "00111111" }, { "SystemId": "00333333" }, { "SystemId": "00444444" } ] Output: [ { "OrderId": "00222222", "BillingCountry": "US", "CurrencyIsoCode": "USD", "PersonEmail": "[email protected]" } ]
    
mule dataweave mulesoft mule4 mule-esb
1个回答
0
投票
此 DataWeave 脚本使用 map 函数从 Array2 中提取所有 SystemId 值。然后,它根据 OrderId 是否包含在 systemIds 数组中来过滤 Array1。这应该排除 Array1 中 OrderId 与 Array2 中任何 SystemId 匹配的元素。

%dw 2.0 var Array2 = [ { "SystemId": "00111111" }, { "SystemId": "00333333" }, { "SystemId": "00444444" } ] var Array1 = [ { "OrderId": "00111111", "BillingCountry": "GB", "CurrencyIsoCode": "GBP", "PersonEmail": "[email protected]" }, { "OrderId": "00222222", "BillingCountry": "US", "CurrencyIsoCode": "USD", "PersonEmail": "[email protected]" } ] output application/json --- Array1 filter ((item1) -> not ((Array2 map ((item) -> item.SystemId)) contains item1.OrderId))
    
© www.soinside.com 2019 - 2024. All rights reserved.