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

数组条件迭代需求:支持元素重复访问与连续遍历处理

带条件的数组迭代实现需求

输入数据

{
    "vals": [
      {
        "month": "Jan 2022",
        "value": 0,
        "amount": -200,
        "date": "01-02-2022"
      },
      {
        "month": "Feb 2022",
        "value": 0,
        "amount": -200,
        "date": "28-02-2022"
      }
    ],
    "items": [
        {
            "date": "01-02-2022",
            "amount": -200
        },
        {
            "date": "04-02-2022",
            "amount": 100
        },
        {
            "date": "28-02-2022",
            "amount": -200
        },
        {
            "date": "10-03-2022",
            "amount": 250
        },
        {
            "date": "12-03-2022",
            "amount": 50
        }
    ]   
}

期望输出数据

{
    "vals": [
      {
        "month": "Jan 2022",
        "value": 37
      },
      {
        "month": "Feb 2022",
        "value": 12
      }
    ]
}

核心处理规则

  • 处理vals中"Jan 2022"条目时:从items中对应日期"01-02-2022"开始遍历,累加amount直到总和匹配该条目的amount值(-200),计算起始日期到终止日期的天数作为value(01-02-2022至10-03-2022共37天)。
  • 处理vals中"Feb 2022"条目时:需从items的上次遍历终止位置继续,而非从头开始。例如items中的{"date": "10-03-2022", "amount": 250}条目,其中100的金额用于Jan 2022,剩余150用于Feb 2022,因此该条目会被两次访问;而{"date": "04-02-2022", "amount": 100}条目仅被访问一次,因其金额不足以覆盖Jan 2022的需求。

内容的提问来源于stack exchange,提问作者Madhuprathap

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.20 02:17:35