Mulesoft:查找数组中的值并根据第二个数组替换内容

Mulesoft: Find for a value in array and replace the contents based on 2nd array

第一个数组 - 包含定价和运输信息的项目集。 注:

  1. 同一个 ItemID 有 2 个 ItemFinalPrices(ItemPrice 和 运费).

  2. 第一个数组中的所有其他值都应出现在 最后留言。

  3. 仅当 ItemID 出现在第二个数组中时 各自的价格应该更新。

    [
     {
         "ItemPrice": {
             "ItemID": "1000300",
             "ItemFinalPrice": 849.98,
             "UnitOfMeasure": "EACH"
         },
         "ShippingPrice": {
             "ItemID": "1000300",
             "ItemFinalPrice": 10.0,
             "UnitOfMeasure": "EACH"
         }
     },
     {
         "ItemPrice": {
             "ItemID": "1000541",
             "ItemFinalPrice": 1849.98,
             "UnitOfMeasure": "EACH"
         },
         "ShippingPrice": {
             "ItemID": "1000541",
             "ItemFinalPrice": 90.0,
             "UnitOfMeasure": "EACH"
         }
     },
     {
         "ItemPrice": {
             "ItemID": "1000549",
             "ItemFinalPrice": 189.98,
             "UnitOfMeasure": "EACH"
         },
         "ShippingPrice": {
             "ItemID": "1000549",
             "ItemFinalPrice": 190.0,
             "UnitOfMeasure": "EACH"
         }
     }
    

    ]

我的第二个数组是一个 ItemId 列表,其中包含更新的定价和运费信息。 此数组不会包含所有项目。它只会包含更新的项目详细信息。

[
  {
    "ItemId": "1000300",
    "UpdatedItemPrice": 99.98,
    "UpdatedShippingAmount": 19.72
  },
  {
    "ItemId": "1000549",
    "UpdatedItemPrice": 199.99,
    "UpdatedShippingAmount": 14.12
  }
]

对于更新的项目,相应的价格信息和其他项目 - 必须映射旧的价格信息。 预期输出:

 [
    {
        "ItemPrice": {
            "ItemID": "1000300", // ItemID is present in updated array, so updated price info to be mapped
            "ItemFinalPrice": 99.98,  //Updated UpdatedItemPrice
            "UnitOfMeasure": "EACH"
        },
        "ShippingPrice": {
            "ItemID": "1000300",
            "ItemFinalPrice": 19.72, //Updated UpdatedShippingAmount
            "UnitOfMeasure": "EACH"
        }
    },
    {
        "ItemPrice": {
            "ItemID": "1000541", //Same ItemId and same price info
            "ItemFinalPrice": 1849.98,
            "UnitOfMeasure": "EACH"
        },
        "ShippingPrice": {
            "ItemID": "1000541",
            "ItemFinalPrice": 90.0,
            "UnitOfMeasure": "EACH"
        }
    },
    {
        "ItemPrice": {
            "ItemID": "1000549", //ItemID is present in updated array, so updated price info to be mapped
            "ItemFinalPrice": 199.99, //Updated UpdatedItemPrice
            "UnitOfMeasure": "EACH"
        },
        "ShippingPrice": {
            "ItemID": "1000549",
            "ItemFinalPrice": 14.12, //Updated UpdatedShippingAmount
            "UnitOfMeasure": "EACH"
        }
    }
]

使用 FirstWith() 和条件更新运算符的解决方案。 payload是第一个数组。

%dw 2.0
output application/json
import * from dw::core::Arrays
var updates=[
  {
    "ItemId": "1000300",
    "UpdatedItemPrice": 99.98,
    "UpdatedShippingAmount": 19.72
  },
  {
    "ItemId": "1000549",
    "UpdatedItemPrice": 199.99,
    "UpdatedShippingAmount": 14.12
  }
]
---
payload map do {
    var thisUpdate= updates firstWith ((update, index) -> update.ItemId == $.ItemPrice.ItemID)
    ---
    $ update {
        case ItemPrice at .ItemPrice.ItemFinalPrice if (thisUpdate != null) -> thisUpdate.UpdatedItemPrice 
        case ShippingPrice at .ShippingPrice.ItemFinalPrice if (thisUpdate != null) ->  thisUpdate.UpdatedShippingAmount 
    }
}

使用 leftJoin 和更新的解决方案:

%dw 2.0
import leftJoin from dw::core::Arrays
output application/json

var actuals = [
    {
        "ItemPrice": {
            "ItemID": "1000300",
            "ItemFinalPrice": 849.98
        },
        "ShippingPrice": {
            "ItemID": "1000300",
            "ItemFinalPrice": 10.0
        }
    },
    {
        "ItemPrice": {
            "ItemID": "1000541",
            "ItemFinalPrice": 1849.98
        },
        "ShippingPrice": {
            "ItemID": "1000541",
            "ItemFinalPrice": 90.0
        }
    },
    {
        "ItemPrice": {
            "ItemID": "1000549",
            "ItemFinalPrice": 189.98
        },
        "ShippingPrice": {
            "ItemID": "1000549",
            "ItemFinalPrice": 190.0
        }
    }
]
var updates = [
  {
    "ItemId": "1000300",
    "UpdatedItemPrice": 99.98,
    "UpdatedShippingAmount": 19.72
  },
  {
    "ItemId": "1000549",
    "UpdatedItemPrice": 199.99,
    "UpdatedShippingAmount": 14.12
  }
]
---
leftJoin(actuals, updates, (actuals) -> actuals.ItemPrice.ItemID, (updates) -> updates.ItemId
) map ( $ update {
    case ip at .l.ItemPrice.ItemFinalPrice -> $.r.UpdatedItemPrice default $.l.ItemPrice.ItemFinalPrice
    case sp at .l.ShippingPrice.ItemFinalPrice -> $.r.UpdatedShippingAmount default $.l.ShippingPrice.ItemFinalPrice
}).l