需要在执行 groupBy 后将相似的客户添加到数组中,但每个客户有限制

Need to add similar customer into array after performing groupBy but with a limit per customer

我昨天问过类似的并得到了正确答案,但今天我遇到了更多问题。 所以这是新的问题陈述。 我正在从 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"
    }
]

我需要输出为一个数组数组,每个子数组应包含最多三个不同客户的所有客户详细信息。 我得到了解决方案,直到上面的语句如下所示

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

现在主要问题, 我们必须限制数组中的客户数量。例如,在给定的输入有效负载中,id 为“1”的客户出现了 4(四)次以上,那么该客户应该在不同的数组中(意味着客户“1”的单独数组,例如五个记录的数组)这可以也发生在任何其他客户身上。

所以我们必须检查客户是否重复了 4 次以上,然后为该类型的客户创建一个单独的数组,否则根据要求将其与其他客户合并

预期输出:

[
    [
        {
            "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"
        }
    ]
]

请验证所有场景,如有遗漏请指正。我只是根据单个数组的大小进行了隔离。

如果大小[array] > 4 then divideBy 5 else divideBy 3.

distinctBy 维护单个有效载荷,因为我在 if 和 else 条件下使用了有效载荷

注意 -> 如您所说的下雨情况将不会维持秩序

%dw 2.0
output application/json
import * from dw::core::Arrays
var a = payload groupBy $.customerID pluck $
---
flatten(a map
    (if (sizeOf($)>4)  
            $ divideBy sizeOf($) map(flatten($))
            else
    (a filter (sizeOf ($)< 5) divideBy 3 map (flatten ($))) 
    ) distinctBy $)

输出

[
  [
    {
      "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": 2,
      "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"
    }
  ]
]

我正要 post 答案,但 Karthik posted,但下面是 @dheerajkain 要求的正确代码

如果任何客户来了四次以上(例如,很快就会有 6 次或 7 次),那么该客户需要一个单独的数组吗?

另外,如您在评论中提到的,如果一个数组有三个客户且每个客户少于四次(每个客户小于或等于 4 次),则没有大小限制?

如果是,请尝试以下代码:

%dw 2.0
import * from dw::core::Arrays
output application/json
var customer = payload groupBy $.customerID pluck $
---
flatten(customer map
    (if (sizeOf($)>4)  
          [flatten($ divideBy 5 map(flatten($)))]
            else
    (customer filter (sizeOf ($)< 5) divideBy 3 map (flatten ($))) 
    ) distinctBy $)