1

我有一个数据集,它是一个对象数组,每个对象都是另一个对象,里面有一个数组。我正在尝试将所有内容展平为最里面的对象的单个数组,而没有中间层对象的键。我曾尝试使用 pluck 功能,但我似乎无法达到我所追求的目标。下面的例子。

输入:

[
  {
    "1234": [
      {
        "store": "07",
        "category": "1234",
        "account": "987"
      },
      {
        "store": "07",
        "category": "1234",
        "account": "555"
      },
      {
        "store": "07",
        "category": "1234",
        "account": "555"
      }
    ]
  },
  {
    "567": [
      {
        "store": "07",
        "category": "567",
        "account": "987"
      },
      {
        "store": "07",
        "category": "567",
        "account": "555"
      },
      {
        "store": "07",
        "category": "567",
        "account": "555"
      }
    ]
  }
]

输出:

[
      {
        "store": "07",
        "category": "1234",
        "account": "987"
      },
      {
        "store": "07",
        "category": "1234",
        "account": "555"
      },
      {
        "store": "07",
        "category": "1234",
        "account": "555"
      },
      {
        "store": "07",
        "category": "567",
        "account": "987"
      },
      {
        "store": "07",
        "category": "567",
        "account": "555"
      },
      {
        "store": "07",
        "category": "567",
        "account": "555"
      }
]
4

2 回答 2

1
  1. 您需要使用map.
  2. 使用 . 将带有键的对象转换为数组pluck
  3. Covnert 嵌套数组到二维数组使用flatten.
  4. 使用将二维数组转换为一维数组 Reduce

德国之声

%dw 2.0
output application/json
---
flatten(payload map ($ pluck $)) reduce ($$++$)

输出

[
  {
    "store": "07",
    "category": "1234",
    "account": "987"
  },
  {
    "store": "07",
    "category": "1234",
    "account": "555"
  },
  {
    "store": "07",
    "category": "1234",
    "account": "555"
  },
  {
    "store": "07",
    "category": "567",
    "account": "987"
  },
  {
    "store": "07",
    "category": "567",
    "account": "555"
  },
  {
    "store": "07",
    "category": "567",
    "account": "555"
  }
]
于 2022-02-18T16:25:05.010 回答
-1

下面是一种方法。可能有比这更好的方法。

%dw 2.0
output application/json
---
flatten(payload map ($ pluck $)) reduce ((val, acc) -> acc ++ val)

于 2022-02-18T16:24:10.833 回答