我有這個 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"
}]
}
謝謝!
uj5u.com熱心網友回復:
假設您的示例資料位于data:
const result = {
"reportResults" : data.rows.map(row => {
return {
[data.columnNames[0]]: row[0],
[data.columnNames[1]]: row[1]
}
})
}
uj5u.com熱心網友回復:
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);
uj5u.com熱心網友回復:
那是我的解決方案:
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})
不是最優的,但很短)
uj5u.com熱心網友回復:
看我上面的評論。提供的每個答案都在瀏覽器中有效,但在我使用的 SOA Suite Javascript 組件中無效。該組件不喜歡 map 函式呼叫。再次感謝所有的答復。
以下是與 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);
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/383240.html
標籤:javascript 数组 json 多维数组
