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

如何通过索引变量在Logic Apps中读取两行数据

动态转换表格JSON为对象数组(无硬编码实现)

输入与目标输出

原始输入JSON:

{"tables": [{"name": "primaryresult","columns": [{"name": "startdate","type": "string"},{"name": "enddate","type": "string"}],"rows": [["01/01/2014","31/01/2014"],["01/02/2014","28/02/2014"]]}]}

需要生成的目标输出:

[ {"startdate" : "01/01/2014", "enddate" : "31/01/2014"}, {"startdate" : "01/02/2014", "enddate" : "28/02/2014"} ]

实现思路(无硬编码,用索引变量关联)

核心是利用索引变量同时关联列名数组和行数据数组的对应位置,不需要硬编码列名(比如startdate、enddate)。以JavaScript为例,实现步骤如下:

  1. 解析输入JSON,提取目标表格的列信息(columns)和行数据(rows)
  2. 遍历每一行数据,对每行数据再遍历列信息,用同一个索引i,把列名作为对象的键,行中对应索引的值作为对象的值
  3. 把每行构建的对象收集到数组中,就是最终结果

代码示例

// 解析输入JSON
const inputJson = '{"tables": [{"name": "primaryresult","columns": [{"name": "startdate","type": "string"},{"name": "enddate","type": "string"}],"rows": [["01/01/2014","31/01/2014"],["01/02/2014","28/02/2014"]]}]}';
const inputData = JSON.parse(inputJson);
const targetTable = inputData.tables[0];
const columnList = targetTable.columns;
const rowList = targetTable.rows;

// 生成目标数组
const outputArray = rowList.map(row => {
  const rowObj = {};
  // 用索引i关联列和行的对应数据
  for (let i = 0; i < columnList.length; i++) {
    rowObj[columnList[i].name] = row[i];
  }
  return rowObj;
});

// 输出结果
console.log(JSON.stringify(outputArray));

说明

这段代码里完全没有硬编码列名,不管columns里有多少列、列名是什么,只要行数据的索引和列的索引对应,就能自动构建出正确的键值对对象,完美匹配需求。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.22 18:34:59