繁体   English   中英

从数组和多维数组创建 JSON object

[英]Create JSON object from Array and Multi-Dimensional Array

我有这个 JSON Object:

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

我想将 Javascript 中的 object 转换为这个 object:

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

我尝试在以下示例中使用 push function:

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);

但是,它似乎像这样创建 object ,并将集合作为单独的数组元素:

{
  [{
    "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"
  }]
}

谢谢!

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

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

一次定义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);

那是我的解决方案:

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 组件中无效。 该组件不喜欢 map function 调用。 再次感谢所有回复。

以下是 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: [] 
}; 
const row = new Object(); 
for (i in process.VWA_Output.rows) { 
  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); 
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM