【问题标题】:How to group by a field in an object to create an array of objects?如何按对象中的字段分组以创建对象数组?
【发布时间】:2023-01-25 22:45:10
【问题描述】:

我有一个包含对象数组的输入负载,我需要在其中 group-by身份证钥匙并根据 id-key 形成 2 个对象数组。请参阅下面的详细信息。

输入载荷:

{
  "id": {
    "header_id": "460",
    "id-branch": {
      "branch-name": "genaral motors",
      "req-name": "genaral motors",
      "id-key": "0791",
      "id-lines": {
        "id-key": "0791",
        "productId": "463"
      }
    },
    "id-branch": {
      "branch-name": "genaral motors",
      "req-name": "genaral motors",
      "id-key": "9692",
      "id-lines": {
        "id-key": "9692",
        "productId": "464"
      },
      "id-lines": {
        "id-key": "9692",
        "productId": "465"
      }
    }
  }
}

期望的输出:

[                  
                   
{
  "branch-name": "genaral motors",
  "req-name": "genaral motors",
  "type": "dhl",
  "lines-ids": "swr",
  "lines": [
    {
      "productId": "463"
    }
  ]
},
{
  "branch-name": "genaral motors",
  "req-name": "genaral motors",
  "type": "dhl",
  "lines-ids": "swr",
  "lines": [
    {
      "productId": "464"
    },
    {
      "productId": "465"
    }
  ]
}
]

必须将输出生成为对象数组,该对象组产品编号它们在相同的 id-key 下。

【问题讨论】:

    标签: mule dataweave mulesoft mule4


    【解决方案1】:

    我使用 filterObject() 删除不需要的键,然后使用 pluck() 将值选取到数组中。之后,我从每个元素中删除了输出中未使用的键。添加的键我认为是文字,因为没有给出任何指示。

    %dw 2.0
    output application/json
    ---
    payload.id
        filterObject ((value, key, index) -> key as String == "id-branch")
        pluck ($) 
        map ($ - "id-key" ++ { "type": "dhl", "lines-ids": "swr"})
    

    输出:

    [
      {
        "branch-name": "genaral motors",
        "req-name": "genaral motors",
        "id-lines": {
          "id-key": "0791",
          "productId": "463"
        },
        "type": "dhl",
        "lines-ids": "swr"
      },
      {
        "branch-name": "genaral motors",
        "req-name": "genaral motors",
        "id-lines": {
          "id-key": "9692",
          "productId": "464"
        },
        "id-lines": {
          "id-key": "9692",
          "productId": "465"
        },
        "type": "dhl",
        "lines-ids": "swr"
      }
    ]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-08-28
      • 2019-09-09
      • 1970-01-01
      • 1970-01-01
      • 2019-07-25
      • 1970-01-01
      • 2018-09-09
      • 1970-01-01
      相关资源
      最近更新 更多