Home > Mobile >  How can I access and process nested arrays on JSON to import on Excel?
How can I access and process nested arrays on JSON to import on Excel?

Time:09-23

Here is the JSON data :-

"data": [
    {
      "character_id": "5785304882",
      "id": 289222,
      "login_status": {
        "created": "2022-09-21T19:52:04.394",
        "status": "Started Competition"
      },
      "name": "user_01",
      "team": "DA ATRAX ESPORTS"
    }

Here is the JS Code :-

gacworksheet.columns = [
  { header: "User Name", key: "name", width: 10 },
  { header: "Team Name", key: "team", width: 10 },
  { header: "Character ID", key: "character_id", width: 10 },
  { header: "Login Time", key: "loginTime", width: 10 },
  { header: "Login Status", key: "loginStatus", width: 10 },
];

var json = JSON.parse(result);
var userData = json['data']; 
userData.forEach(user => {
  for (var i = 0; i < userData.length; i  ) {
    var loginTime = userData[i].login_status.created;
    var loginStatus = userData[i].login_status.status;
  }
   gacworksheet.addRow(user);
});
let gacdata = gacworksheet.getSheetValues();
gacMData = gacdata.map(function(r){
  return [r[1],r[2],r[3],r[4],r[5]];
});
console.log(gacMData);

When I console.log the data, the data which is inside the array is undefined. How to set the array value to a Key?

Result of console.log(gacMData);

[
    'User Name',
    'Team Name',
    'Character ID',
    'Login Time',
    'Login Status'
  ],
  [ 'user_01', 'DA ATRAX ESPORTS', '5785304882', undefined, undefined ],
  [ 'user_02', 'DA ATRAX ESPORTS', '5670142641', undefined, undefined ],

CodePudding user response:

userData.forEach(user => {
    let newUser = {...user}
    newUser.loginTime = user.login_status.created;
    newUser.loginStatus = user.login_status.status
    gacworksheet.addRow(newUser);
  });
  • Related