Home > Net >  Create JSON object from Array and Multi-Dimensional Array
Create JSON object from Array and Multi-Dimensional Array

Time:12-16

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!

CodePudding user response:

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

CodePudding user response:

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

CodePudding user response:

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)

  • Related