【问题标题】:Compare multiple fields in a single array and have conditions on their values比较单个数组中的多个字段并对其值具有条件
【发布时间】:2022-01-29 18:24:09
【问题描述】:

我需要比较一个数组中的两个字段,并用不同的数据替换它们的值并在不同的对象中输出

这是我的示例输入数组:

{
    "data": [
    {
      "house": "house1",
      "condition": "bad",
      "age": "old",

    },
    {
      "house": "house2",
      "condition": "good",
      "age": "new",
    }
  ]
}

输出应该是

  {
      "data": [
        {
          "house": "house1",
          "condition": "repair"
    
        },
        {
          "house": "house1",
          "age": "over50",
        },
        {
          "house": "house2",
          "condition": "No repair",
        },
        {
          "house": "house2",
          "age": "recent"
        }
      ]
    }

如果条件“坏”,我需要用“修复”替换,否则如果条件“好”,我需要用“不修复”替换。 同类型的逻辑。对于年龄领域。如果年龄是“旧”,我需要替换为“Over50”,如果年龄是“新”,我需要替换为“最近”。这两个字段(年龄和条件)将在每次迭代中出现

【问题讨论】:

    标签: mule mule-studio dataweave mulesoft mule4


    【解决方案1】:

    正如 aled 所建议的,Map 将与 OrderBy 一起执行此操作。

    %dw 2.0
    output application/json
    var a=payload.data
    ---
    "data":(a map{
        "house": $.house,
        "condition":if (($.condition)=="bad") "repair" else "No repair"
    } ++ (a map{
        "house": $.house,
        "age":if (($.age)=="old") "over50" else "recent"
    })orderBy $.house)
    

    输出

    {
      "data": [
        {
          "house": "house1",
          "condition": "repair"
        },
        {
          "house": "house1",
          "age": "over50"
        },
        {
          "house": "house2",
          "condition": "No repair"
        },
        {
          "house": "house2",
          "age": "recent"
        }
      ]
    }
    

    【讨论】:

      【解决方案2】:

      使用单个 map 的替代解决方案(映射到所需的 2 个对象的数组,然后展平结果):

      %dw 2.0
      output application/json
      fun mapCondition(cond : String) = if (cond == "bad") "repair" else "No repair"
      fun mapAge(age : String) = if (age == "old") "over50" else "recent"
      ---
      { 
          data : flatten(payload.data 
              map ((item, index) -> 
              [   
                  {
                      house: item.house, 
                      condition: mapCondition(item.condition)
                  }, 
                  {
                      house: item.house, 
                      age: mapAge(item.age)
                  }
              ]))
      }
      

      【讨论】:

        【解决方案3】:

        在数据数组上使用 map() 并使用 if 条件来转换地图内每个元素的字段。看起来很直接。

        【讨论】:

        • if 条件的问题是,如果第一个条件匹配(条件字段),那么它不会转到第二个条件(年龄字段)。如果对象中存在两个字段,我需要输出它们
        • 我刚刚编辑了我需要打印的实际输出。条件匹配后,这两个字段需要在单独的对象中。
        【解决方案4】:

        您还可以通过调用 flatMap 来应用地图和展平。

        %dw 2.0
        output application/json
        ---
        data: payload.data flatMap [
            {
                house: $.house,
                condition: $.condition match {
                    case 'bad' -> 'Repair'
                    else -> 'No Repair'
                }
            },
            {
                house: $.house,
                age: $.age match {
                    case 'old' -> 'over50'
                    else -> 'new'
                }
            }
        ]
        

        【讨论】:

        • 我试过这个脚本,它成功了。谢谢@MrM
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-07-22
        • 1970-01-01
        • 2022-12-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多