You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Azure Logic App中使用Until循环检测凭证数组中是否存在canBeUsed为true的对象并终止执行

在Azure Logic Apps中用Until循环终止于第一个canBeUsed为true的凭证

嘿,我完全懂你要实现的逻辑——就像C#里的response.Any(b => b.CanBeUsed)那样,找到第一个canBeUsed为true的凭证就立刻停止遍历,不用挨个过所有元素。下面我一步步给你讲怎么在Until循环里搞定这个需求:

第一步:初始化核心变量

首先得创建两个变量来跟踪遍历状态:

  • currentIndex:整数类型,初始值设为0,用来记录当前遍历到数组的第几个元素
  • foundUsableVoucher:布尔类型,初始值设为false,用来标记是否找到了符合条件的凭证
  • 额外建议:把请求体里的vouchers数组单独存到一个变量里(比如也叫vouchers),用表达式triggerBody()?['vouchers']赋值,这样后续引用更稳定,避免重复解析请求体

第二步:配置Until循环的终止条件

你的循环名称可以保留“Until Customer Has No Vouchers That Can Be Used”,但终止条件要设置成满足任一条件就停止循环:

  1. variables('foundUsableVoucher') 等于 true(找到可用凭证,直接终止)
  2. variables('currentIndex') 大于等于 length(variables('vouchers'))(遍历完所有凭证都没找到,自然终止)

这两个条件要设为「或」关系,确保不会出现无限循环的情况。

第三步:循环内部的处理逻辑

在Until循环里按顺序添加以下动作:

  1. 获取当前索引的凭证:添加一个Compose动作(可以命名为Get Current Voucher),输入表达式:
    variables('vouchers')[variables('currentIndex')]
    
    这样就能拿到当前遍历的凭证对象
  2. 检查canBeUsed属性:添加一个Condition动作,判断表达式:
    outputs('Get_Current_Voucher')?['canBeUsed']
    
    是否等于true
    • 如果条件成立(找到可用凭证):用Set variable动作把foundUsableVoucher设为true,下一次循环就会触发终止条件
    • 如果条件不成立:用Set variable动作把currentIndex的值加1(表达式variables('currentIndex') + 1),继续遍历下一个元素

验证逻辑

用你提供的测试JSON数据(我修正了一处日期错误:把2020-09-31改成了合法的2020-09-30)来验证:

{
  "vouchers": [
    {
      "amountPending": {
        "currency": {
          "code": "EUR",
          "symbol": "€",
          "id": 978
        },
        "amount": 1517
      },
      "amountPendingWithoutBonus": {
        "currency": {
          "code": "EUR",
          "symbol": "€",
          "id": 978
        },
        "amount": 1517
      },
      "canBeUsed": false,
      "cannotBeUsedReasons": [],
      "firstName": "jason",
      "lastName": "denis",
      "expirationDate": "2021-11-30T00:00:00",
      "issuedDate": "2020-05-31T00:00:00",
      "issuedReason": "Cancellation",
      "voucherType": "Covid19",
      "usages": []
    },
    {
      "amountPending": {
        "currency": {
          "code": "EUR",
          "symbol": "€",
          "id": 123
        },
        "amount": 134
      },
      "amountPendingWithoutBonus": {
        "currency": {
          "code": "EUR",
          "symbol": "€",
          "id": 9718
        },
        "amount": 551
      },
      "canBeUsed": true,
      "cannotBeUsedReasons": [],
      "firstName": "paul",
      "lastName": "frank",
      "expirationDate": "2021-12-30T00:00:00",
      "issuedDate": "2020-09-30T00:00:00",
      "issuedReason": "Cancellation",
      "voucherType": "Covid19",
      "usages": []
    }
  ]
}

第一次循环时currentIndex是0,拿到第一个凭证,canBeUsed为false,所以currentIndex变成1;第二次循环拿到第二个凭证,canBeUsed为true,foundUsableVoucher设为true,循环立刻终止,完全符合你要的短路遍历效果。

内容的提问来源于stack exchange,提问作者Arnau Van Boschken ArnauB

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.01 00:12:39