繁体   English   中英

Mule 3.8.4 中的 for 循环 | 数据编织 1.0

[英]for loops in Mule 3.8.4 | Dataweave 1.0

我需要对传入的有效负载字符串与缓存中的现有产品名称列表进行字符串比较,因为不会有完全匹配,首先我需要从传入的有效负载字符串中删除最后 3 个字符并与所有字符进行比较现有的字符串/产品名称。 如果我找到匹配项,则进行其他转换。 否则从传入的有效负载字符串中再删除一个字符并再次比较。 我需要重复此操作,直到传入有效负载的字符串大小为 =3(如果字符串大小小于 3,则无需比较)

总结:我需要从右到左删除字符并进行字符串比较

输入有效载荷如下:

{
  "productPartNo": "MPN-400110",
  "supplier": "70058",
  "productCode": "02",
}

需要将字符串“productPartNo”与下面现有产品列表中的“partNumber”字段进行比较

{
  "Product": [
    {
      "productNumber": "420475",
      "created": "2012-10-28",
      "partNumber": "C1F2PNEX71K9",
      "codeNo": "7712",
      "manufacturer": ""
    },
    {
      "productNumber": "478376",
      "created": "2017-12-12",
      "partNumber": "N77-C77-RMK=",
      "codeNo": "1589",
      "manufacturer": "50884"
    },
    {
      "productNumber": "478381",
      "created": "2017-12-12",
      "partNumber": "CON-U-C1F2PXNE",
      "codeNo": "1586",
      "manufacturer": "50884"
    },
    {
      "productNumber": "478384",
      "created": "2017-12-12",
      "partNumber": "PMPN4070",
      "codeNo": "1585",
      "manufacturer": "50884"
    }
 ]
}

在这种情况下,我的输入字符串 "productPartNo": "MPN-400110" 应该与所有 PartNumber 进行比较。 从最后删除字符后,字符串 'MPN' 将与“partNumber”匹配/包含:“PMPN4070”,因此我应该得到与 partNumber-PMPN4070 关联的“codeNo”:“1585”作为我的输出字段。

我如何在 %dw 1.0 中实现这个逻辑

这需要一点思考,而且它很晚,没有多少脑力了,早上我可能会修改,但鉴于您的样本数据,这是一个好的开始。 如果您有任何进一步的说明,请告诉我。

只需复制并粘贴到Transform Message处理器中,然后打开预览即可。

%dw 1.0
%output application/dw

%var products = {
  "Product": [
    {
      "productNumber": "420475",
      "created": "2012-10-28",
      "partNumber": "C1F2PNEX71K9",
      "codeNo": "7712",
      "manufacturer": ""
    },
    {
      "productNumber": "478376",
      "created": "2017-12-12",
      "partNumber": "N77-C77-RMK=",
      "codeNo": "1589",
      "manufacturer": "50884"
    },
    {
      "productNumber": "478381",
      "created": "2017-12-12",
      "partNumber": "CON-U-C1F2PXNE",
      "codeNo": "1586",
      "manufacturer": "50884"
    },
    {
      "productNumber": "478384",
      "created": "2017-12-12",
      "partNumber": "PMPN4070",
      "codeNo": "1585",
      "manufacturer": "50884"
    }
 ]
}


%var inputPayload = {
  "productPartNo": "MPN-400110",
  "supplier": "70058",
  "productCode": "02"
}
---
// Get a list with all the substrings to search with--this give you the following array
// ["MPN-400","MPN-40","MPN-4","MPN-","MPN"]
(4 to (sizeOf inputPayload.productPartNo ) - 2 map inputPayload.productPartNo[0 to -$]
map (
    // Filter the list of products on the substring, 
    // this should either give you a matches or the empty list
    // Finally, take the first match.  I am not sure if this what you want.
    (products.Product filter (e) -> (e.partNumber contains $))[0]
) 
filter ($ != null))[0].codeNo

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM