从数组和多维数组创建 JSON 对象

Create JSON object from Array and Multi-Dimensional Array

我有这个 JSON 对象:

{
  "columnNames": [
    "Incident ID",
    "IncidentType"
  ],
  "rows": [
    [
      "3599590",
      "Telecommuting/VWA Empl- Initiate"
    ],
    [
      "3599601",
      "Telecommuting/VWA Empl- Initiate"
    ]
  ]
}

我想将 Javascript 中的那个对象转换成这个对象:

{
  reportResults: [{
      "Incident ID": "3599590",
      "IncidentType": "Telecommuting/VWA Empl- Initiate"
    },
    {
      "Incident ID": "3599591",
      "IncidentType": "Telecommuting/VWA Empl- Initiate"
    }
  ]
}

我尝试在以下示例中使用推送功能:

VWA_Output = {
  "columnNames": [
    "Incident ID",
    "IncidentType"
  ],
  "rows": [
    [
      "3599590",
      "Telecommuting/VWA Empl- Initiate"
    ],
    [
      "3599601",
      "Telecommuting/VWA Empl- Initiate"
    ]
  ]
};

JSTest_JSON_Var1 = {
  reportResults: []
};
for (i in VWA_Output.rows) {
  for (var j in VWA_Output.rows[i]) {
    var key = VWA_Output.columnNames[j];
    var value = VWA_Output.rows[i][j]
    JSTest_JSON_Var1.reportResults.push({
      [key]: value
    });

  }
}
console.log(JSTest_JSON_Var1);

但是,它似乎是用集合作为单独的数组元素来创建这样的对象:

{
  [{
    "reportResults": [{
        "Incident ID": "3599590"
      }, {
        "IncidentType": "Telecommuting/VWA Empl- Initiate"
      }
    },
    {
      "Incident ID": "3599591"
    },
    {
      "IncidentType": "Telecommuting/VWA Empl- Initiate"
    }
  }]
}

我希望列和行的集合成为数组中的单个记录集合:

{
  "reportResults": [{
    "Incident ID": "3599590",
    "IncidentType": "Telecommuting/VWA Empl- Initiate"
  }, {
    "Incident ID": "3599591",
    "IncidentType": "Telecommuting/VWA Empl- Initiate"
  }]
}

谢谢!

一次性定义 reportResults - 将其内容设为映射自 rows 的数组,您可以在其中使用要迭代的列名的索引来访问相应的行价值。我会使用 Object.fromEntries 来保持简洁。

const input = {
  "columnNames": [
    "Incident ID",
    "IncidentType"
  ],
  "rows": [
    [
      "3599590",
      "Telecommuting/VWA Empl- Initiate"
    ],
    [
      "3599601",
      "Telecommuting/VWA Empl- Initiate"
    ]
  ]
};
const output = {
  reportResults: input.rows.map(row => Object.fromEntries(
    input.columnNames.map((name, i) => [name, row[i]])
  ))
};
console.log(output);

假设您的示例数据位于 data:

const result = {
  "reportResults" : data.rows.map(row => {
    return {
      [data.columnNames[0]]: row[0],
      [data.columnNames[1]]: row[1]
    }
  })
}

这是我的解决方案:

var data = {
  "columnNames": [
    "Incident ID",
    "IncidentType"
  ],
  "rows": [
    [
      "3599590",
      "Telecommuting/VWA Empl- Initiate"
    ],
    [
      "3599601",
      "Telecommuting/VWA Empl- Initiate"
    ]
  ]
}

var reportResults = data.rows.map((row) => 
    Object.assign({}, ...row.map((cell, i) => ({ [data.columnNames[i]]: cell})))
)

console.log({reportResults})

不是最优的,但很短)

请参阅我上面的评论。提供的每个答案都在浏览器中有效,但在我使用的 SOA Suite Javascript 组件中无效。该组件不喜欢地图函数调用。再次感谢大家的回复。

以下是与 Oracle SOA Suite JS 组件一起工作的内容:

process.VWA_Output = {
  "columnNames": [
    "Incident ID",
    "IncidentType"
  ],
  "rows": [
    [
      "3599590",
      "Telecommuting/VWA Empl- Initiate"
    ],
    [
      "3599601",
      "Telecommuting/VWA Empl- Initiate"
    ]
  ]
};
process.JSTest_JSON_Var1 = { 
  reportResults: [] 
}; 

for (i in process.VWA_Output.rows) { 
  const row = new Object(); 
  for (var j in process.VWA_Output.rows[i]) { 
    var key = process.VWA_Output.columnNames[j]; 
    var value = process.VWA_Output.rows[i][j]; 
    row[key] = value; 
  } 
  process.JSTest_JSON_Var1.reportResults.push(row); 
}