0

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 ],
5
  • What do you get, when you log gacdata? Commented Sep 22, 2022 at 4:45
  • [ 'User Name', 'Team Name', 'Character ID', 'Login Time', 'Login Status' ], [ 'user_01', 'DA ATRAX ESPORTS', '5785304882', undefined, undefined ], Commented Sep 22, 2022 at 4:46
  • So, you get an array of arrays? Commented Sep 22, 2022 at 4:49
  • In place of undefined I want to get login_status.created and login_status.status Commented Sep 22, 2022 at 4:50
  • @Yogi there is multiple data in Json file and I'm using userData.forEach to add the data in worksheet. Commented Sep 22, 2022 at 5:38

1 Answer 1

1
userData.forEach(user => {
    let newUser = {...user}
    newUser.loginTime = user.login_status.created;
    newUser.loginStatus = user.login_status.status
    gacworksheet.addRow(newUser);
  });
Sign up to request clarification or add additional context in comments.

5 Comments

Yes the data comes when you console.log inside the userData.forEach, But i want that data in gacMData instead of undefined.
Could you please share addRow Function (gacworksheet.addRow(user)) and getSheetValues function (gacworksheet.getSheetValues()) ? So that i can run the code and my end and check the issue.
it is the function of exceljs const gacworkbook = new excelJS.Workbook(); const gacworksheet = gacworkbook.addWorksheet("GAC Data");
I have edit my answer. Just replace the forEach loop with this code. With this i am able to console the expected values in gacMData. I hope this will help.
If it helped you, please accept my answer as correct and give rating to it. Thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.