简体   繁体   中英

Create JSON object from Array and Multi-Dimensional Array

I have this JSON Object:

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

I would like to convert that object in Javascript to this object:

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

I have tried using the push function in the following example:

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

However, it seems to create the the object like this with the collection as an individual array element:

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

I would like the collection of columns and rows to be a single record collection in the array:

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

Thanks!

Suppose your example data is in data :

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

Define the reportResults all at once - have its contents be an array mapped from the rows , where you use the index of the column name you're iterating over to access the appropriate row value. I'd use Object.fromEntries to keep things concise.

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

that is my solution:

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

Not optimal, but short)

See my comments above. Every answer provided worked in the browser, but not in the SOA Suite Javascript component I am using. The component didn't like the map function calls. Thanks again for all of the responses.

Here is what did work with the Oracle SOA Suite JS component:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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