执行GROUP BY后,需要将相似客户添加到阵列中 [英] Need to add similar customer into array after performing grouBy

查看:0
本文介绍了执行GROUP BY后,需要将相似客户添加到阵列中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从Salesforce获取客户联系人,这些联系人以对象数组的形式出现,如下所示

[
    {
        "customerID": 1,
        "customerName": "Jonhn1"
    },
    {
        "customerID": 1,
        "customerName": "Jonhn2"
    },
    {
        "customerID": 1,
        "customerName": "Jonhn3"
    },
    {
        "customerID": 1,
        "customerName": "Jonhn4"
    },
    {
        "customerID": 1,
        "customerName": "Jonhn5"
    },
    {
        "customerID": 2,
        "customerName": "Jonhn6"
    },
    {
        "customerID": 2,
        "customerName": "Jonhn7"
    },
    {
        "customerID": 2,
        "customerName": "Jonhn8"
    },
    {
        "customerID": 3,
        "customerName": "Jonhn9"
    },
    {
        "customerID": 3,
        "customerName": "Jonhn10"
    },
    {
        "customerID": 3,
        "customerName": "Jonhn11"
    },
    {
        "customerID": 4,
        "customerName": "Jonhn12"
    },
    {
        "customerID": 4,
        "customerName": "Jonhn13"
    },
    {
        "customerID": 5,
        "customerName": "Jonhn14"
    },
    {
        "customerID": 5,
        "customerName": "Jonhn15"
    },
    {
        "customerID": 5,
        "customerName": "Jonhn16"
    },
    {
        "customerID": 6,
        "customerName": "Jonhn17"
    },
    {
        "customerID": 7,
        "customerName": "Jonhn17"
    }
]

我需要输出为数组数组,每个子数组最多应包含三个不同客户的所有客户详细信息。

流水线产量:

[
    [
        {
            "customerID": 1,
            "customerName": "Jonhn1"
        },
        {
            "customerID": 1,
            "customerName": "Jonhn2"
        },
        {
            "customerID": 1,
            "customerName": "Jonhn3"
        },
        {
            "customerID": 1,
            "customerName": "Jonhn4"
        },
        {
            "customerID": 1,
            "customerName": "Jonhn5"
        },
        {
            "customerID": 2,
            "customerName": "Jonhn6"
        },
        {
            "customerID": 2,
            "customerName": "Jonhn7"
        },
        {
            "customerID": 2,
            "customerName": "Jonhn8"
        },
        {
            "customerID": 3,
            "customerName": "Jonhn9"
        },
        {
            "customerID": 3,
            "customerName": "Jonhn10"
        },
        {
            "customerID": 3,
            "customerName": "Jonhn11"
        }
    ],
    [
        {
            "customerID": 4,
            "customerName": "Jonhn12"
        },
        {
            "customerID": 4,
            "customerName": "Jonhn13"
        },
        {
            "customerID": 5,
            "customerName": "Jonhn14"
        },
        {
            "customerID": 5,
            "customerName": "Jonhn15"
        },
        {
            "customerID": 5,
            "customerName": "Jonhn16"
        },
        {
            "customerID": 6,
            "customerName": "Jonhn17"
        }
    ],
    [
        {
            "customerID": 7,
            "customerName": "Jonhn18"
        }
    ]
]

我需要将此数组数组发送到PARALLEL FOR以进行并行处理。

推荐答案

GroupBy根据客户ID分组。

pluck用于将键为数字的对象转换为数组。

divideBy 3用于分组,例如[[1],[2],[3]],[[4],[5],[6]],[[7]

map用于循环访问嵌套数组[对象数组]

flatten将嵌套数组[]转换为单数组[[]]

%dw 2.0
output application/json
import * from dw::core::Arrays
---
payload groupBy $.customerID pluck $ divideBy 3 map((flatten($)))

输出

[
  [
    {
      "customerID": 1,
      "customerName": "Jonhn1"
    },
    {
      "customerID": 1,
      "customerName": "Jonhn2"
    },
    {
      "customerID": 1,
      "customerName": "Jonhn3"
    },
    {
      "customerID": 1,
      "customerName": "Jonhn4"
    },
    {
      "customerID": 1,
      "customerName": "Jonhn5"
    },
    {
      "customerID": 2,
      "customerName": "Jonhn6"
    },
    {
      "customerID": 2,
      "customerName": "Jonhn7"
    },
    {
      "customerID": 2,
      "customerName": "Jonhn8"
    },
    {
      "customerID": 3,
      "customerName": "Jonhn9"
    },
    {
      "customerID": 3,
      "customerName": "Jonhn10"
    },
    {
      "customerID": 3,
      "customerName": "Jonhn11"
    }
  ],
  [
    {
      "customerID": 4,
      "customerName": "Jonhn12"
    },
    {
      "customerID": 4,
      "customerName": "Jonhn13"
    },
    {
      "customerID": 5,
      "customerName": "Jonhn14"
    },
    {
      "customerID": 5,
      "customerName": "Jonhn15"
    },
    {
      "customerID": 5,
      "customerName": "Jonhn16"
    },
    {
      "customerID": 6,
      "customerName": "Jonhn17"
    }
  ],
  [
    {
      "customerID": 7,
      "customerName": "Jonhn17"
    }
  ]
]

这篇关于执行GROUP BY后,需要将相似客户添加到阵列中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆