如何将 foreach 循环的结果连接或合并到一个数组中?

How do I join or combine the results of a foreach loop into an array?

我有一个 for each 操作,它接收一个 JSON 数组并添加一个 Item:Key 对。

for each 的输入如下所示:

[
  {
    "memberName": "G-TEL FOR ENBRIDGE GAS (LEGACY UNION GAS) (ENOW01)",
    "stationCode": "ENOW01",
    "initialStatus": "Notification sent"
  },
  {
    "memberName": "G-TEL FOR CITY OF BRANTFORD - STREETLIGHTS (BRASL01)",
    "stationCode": "BRASL01",
    "initialStatus": "Notification sent"
  },
  {
    "memberName": "G-TEL FOR BRANTFORD POWER (LOCAL HYDRO) (BRAP01)",
    "stationCode": "BRAP01",
    "initialStatus": "Notification sent"
  },
  {
    "memberName": "G-TEL FOR CITY OF BRANTFORD - SEWER/WATER/STORM (BRAS01)",
    "stationCode": "BRAS01",
    "initialStatus": "Cleared"
  },
  {
    "memberName": "G-TEL FOR BELL CANADA (BCOW01)",
    "stationCode": "BCOW01",
    "initialStatus": "Notification sent"
  }
]

我在 compose 中使用它从变量中插入一个值:array(union(items('For_each_3'),outputs('Compose_8'))) 并得到这个结果:

[
  {
    "memberName": "G-TEL FOR ENBRIDGE GAS (LEGACY UNION GAS) (ENOW01)",
    "stationCode": "ENOW01",
    "initialStatus": "Notification sent",
    "LocateNumber": "2022054487"
  }
]

相同的值被插入到每个对象中。现在我需要合并结果,以便在 Excel 电子表格中添加行。

我边走边想,我试图使用我在别处找到的这个表达式将结果组合回一个数组:join(items('For_each_3'),',')。但是我得到一个错误,Join 期望第一个参数是一个数组。我认为每个结果已经是一个数组。我一直在寻找答案,但找不到。

正如我提到的,我希望输出到 excel 电子表格,所以如果有更好的方法来做到这一点,我很乐意听到它,但我仍然想知道如何加入结果也只是为了我自己的知识。谢谢。

这是解决方法之一。因为你想将数据保存到你的 excel 我已经直接使用 excel 作为业务连接器(你可以根据你的要求使用任何 excel 相关的连接器)。

这是我的逻辑应用程序

输出:-

这是我生成的 excel 文件

如果我没理解错的话,你想在你的原始数组中的每个项目中添加相同的“LocateNumber”,如果是这样的话,我设法得到了一些有用的东西但是,它确实需要特定的设置才能正常工作。

我创建了一个新数组对象,然后将每个 updated/new 项添加到该新数组中。 Excel 方面,我还没有解决,但这回答了关于你遇到的问题的直接问题。

向您展示的最简单方法是为您提供 JSON 定义,以便您可以在自己的租户中重新创建它。

{
    "definition": {
        "$schema": "https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#",
        "actions": {
            "For_Each_Array_Item": {
                "actions": {
                    "Append_Updated_Array_Item_Object_to_New_JSON_Array": {
                        "inputs": {
                            "name": "Updated JSON Array",
                            "value": "@variables('New Array Item Object')"
                        },
                        "runAfter": {
                            "Update_New_Array_Item_Object": [
                                "Succeeded"
                            ]
                        },
                        "type": "AppendToArrayVariable"
                    },
                    "Set_Array_Item_Object": {
                        "inputs": {
                            "name": "Array Item Object",
                            "value": "@items('For_Each_Array_Item')"
                        },
                        "runAfter": {},
                        "type": "SetVariable"
                    },
                    "Update_New_Array_Item_Object": {
                        "inputs": {
                            "name": "New Array Item Object",
                            "value": "@addProperty(variables('Array Item Object'), 'LocateNumber', '2022054487')"
                        },
                        "runAfter": {
                            "Set_Array_Item_Object": [
                                "Succeeded"
                            ]
                        },
                        "type": "SetVariable"
                    }
                },
                "foreach": "@variables('JSON')",
                "runAfter": {
                    "Initialize_Updated_JSON_Array": [
                        "Succeeded"
                    ]
                },
                "runtimeConfiguration": {
                    "concurrency": {
                        "repetitions": 1
                    }
                },
                "type": "Foreach"
            },
            "Initialize_Array_Item_Object": {
                "inputs": {
                    "variables": [
                        {
                            "name": "Array Item Object",
                            "type": "object"
                        }
                    ]
                },
                "runAfter": {
                    "Initialize_JSON_Array": [
                        "Succeeded"
                    ]
                },
                "type": "InitializeVariable"
            },
            "Initialize_Debug_Array": {
                "inputs": {
                    "variables": [
                        {
                            "name": "Debug Array",
                            "type": "array",
                            "value": "@variables('Updated JSON Array')"
                        }
                    ]
                },
                "runAfter": {
                    "For_Each_Array_Item": [
                        "Succeeded"
                    ]
                },
                "type": "InitializeVariable"
            },
            "Initialize_JSON_Array": {
                "inputs": {
                    "variables": [
                        {
                            "name": "JSON",
                            "type": "array",
                            "value": [
                                {
                                    "initialStatus": "Notification sent",
                                    "memberName": "G-TEL FOR ENBRIDGE GAS (LEGACY UNION GAS) (ENOW01)",
                                    "stationCode": "ENOW01"
                                },
                                {
                                    "initialStatus": "Notification sent",
                                    "memberName": "G-TEL FOR CITY OF BRANTFORD - STREETLIGHTS (BRASL01)",
                                    "stationCode": "BRASL01"
                                },
                                {
                                    "initialStatus": "Notification sent",
                                    "memberName": "G-TEL FOR BRANTFORD POWER (LOCAL HYDRO) (BRAP01)",
                                    "stationCode": "BRAP01"
                                },
                                {
                                    "initialStatus": "Cleared",
                                    "memberName": "G-TEL FOR CITY OF BRANTFORD - SEWER/WATER/STORM (BRAS01)",
                                    "stationCode": "BRAS01"
                                },
                                {
                                    "initialStatus": "Notification sent",
                                    "memberName": "G-TEL FOR BELL CANADA (BCOW01)",
                                    "stationCode": "BCOW01"
                                }
                            ]
                        }
                    ]
                },
                "runAfter": {},
                "type": "InitializeVariable"
            },
            "Initialize_New_Array_Item_Object": {
                "inputs": {
                    "variables": [
                        {
                            "name": "New Array Item Object",
                            "type": "object"
                        }
                    ]
                },
                "runAfter": {
                    "Initialize_Array_Item_Object": [
                        "Succeeded"
                    ]
                },
                "type": "InitializeVariable"
            },
            "Initialize_Updated_JSON_Array": {
                "inputs": {
                    "variables": [
                        {
                            "name": "Updated JSON Array",
                            "type": "array"
                        }
                    ]
                },
                "runAfter": {
                    "Initialize_New_Array_Item_Object": [
                        "Succeeded"
                    ]
                },
                "type": "InitializeVariable"
            }
        },
        "contentVersion": "1.0.0.0",
        "outputs": {},
        "parameters": {},
        "triggers": {
            "Recurrence": {
                "evaluatedRecurrence": {
                    "frequency": "Month",
                    "interval": 3
                },
                "recurrence": {
                    "frequency": "Month",
                    "interval": 3
                },
                "type": "Recurrence"
            }
        }
    },
    "parameters": {}
}

流程的基础是...

  1. 创建存储现有数组、正在处理的数组项、new/updated 项和新数组的变量。
  2. 处理数组中的每一项,将当前项存储在一个新变量中(已为 debugging/visibility 包含此项),然后用新的 属性 更新该项,最后将其添加到新数组。

该公式中的表达式(虽然在附加的定义中找到)看起来像这样...

addProperty(variables('Array Item Object'), 'LocateNumber', '2022054487')

关键设置

For Each 必须将并发切换为 1。它不喜欢打开它,它在重复项目的新数组中不断出现错误输出。

转到操作上的 Settings 并使其看起来像下面这样。

流程中的最后一步只是一个新变量,它有助于可视化流程运行后的输出。

[
  {
    "initialStatus": "Notification sent",
    "memberName": "G-TEL FOR ENBRIDGE GAS (LEGACY UNION GAS) (ENOW01)",
    "stationCode": "ENOW01",
    "LocateNumber": "2022054487"
  },
  {
    "initialStatus": "Notification sent",
    "memberName": "G-TEL FOR CITY OF BRANTFORD - STREETLIGHTS (BRASL01)",
    "stationCode": "BRASL01",
    "LocateNumber": "2022054487"
  },
  {
    "initialStatus": "Notification sent",
    "memberName": "G-TEL FOR BRANTFORD POWER (LOCAL HYDRO) (BRAP01)",
    "stationCode": "BRAP01",
    "LocateNumber": "2022054487"
  },
  {
    "initialStatus": "Cleared",
    "memberName": "G-TEL FOR CITY OF BRANTFORD - SEWER/WATER/STORM (BRAS01)",
    "stationCode": "BRAS01",
    "LocateNumber": "2022054487"
  },
  {
    "initialStatus": "Notification sent",
    "memberName": "G-TEL FOR BELL CANADA (BCOW01)",
    "stationCode": "BCOW01",
    "LocateNumber": "2022054487"
  }
]

如果您认为该解决方案可以接受,我相信您可以将其改编成您自己的流程。