'Accessing values inside Objects

I am trying to print a 2d array of a schedule that is inputted to me via object. When it prints, it is not giving me the values I want. Any idea what i doing wrong when digging into the object values?

Heres the schedule given to me:

var schedule = {
"Mon": [undefined, "Bowser", undefined, undefined, undefined, "Ladykiller"],
"Tues": [undefined, undefined, "Fifi", undefined, undefined, undefined],
"Wed": [undefined, undefined, undefined, "Spike", undefined, undefined],
"Thurs":[undefined, undefined, undefined, undefined, undefined]
}

Heres my code trying to add it to an array and print it:

function printSchedule(schedule){

var mySchedule = [["    ", "Mon   ", "Tue   ", "Wed   ", "Thu   "],
                ["9am", "      ", "      ", "      ", "      "],
                ["10am", "      ", "      ", "      ", "      "],
                ["11am", "      ", "      ", "      ", "      "],
                ["12am", "      ", "      ", "      ", "      "],
                ["1pm", "      ", "      ", "      ", "      "],
                ["2pm", "      ", "      ", "      ", "      "]];


for(var property in schedule){

  if(property === "Mon"){
    for(var i = 0; i < property.length; i++){
      if(property[i] !== undefined){
        mySchedule[1][i + 1] = property[i];
      }
    }
  }

  else if(property === "Tue"){
    for(var i = 0; i < property.length; i++){
      if(property[i] !== undefined){
        mySchedule[2][i + 1] = property[i];
      }
    }
  }

  else if(property === "Wed"){
    for(var i = 0; i < property.length; i++){
      if(property[i] !== undefined){
        mySchedule[3][i + 1] = property[i];
      }
    }
  }

  else if(property === "Thu"){
    for(var i = 0; i < property.length; i++){
      if(property[i] !== undefined){
        mySchedule[4][i + 1] = property[i];
      }
    }
  }
}
console.table(mySchedule)
}

Current output is giving me things like "M" "o" "T" when I really just want the string that was inside the object. Thanks!



Solution 1:[1]

[edit] Wow, someone actually edited a five year old question...

There is a lot of redundant (copy pasted) code. So I wrote a shorter script, and flipped the table to make it it easier to parse the schedule object.

printTimetable() is mostly to showcase the output. I'm assuming that you want to print this to an HTML table so the formatting doesn't really matter for now.

var schedule = {
  "Mon":   [undefined, "Bowser", undefined, undefined, undefined, "Ladykiller"],
  "Tues":  [undefined, undefined, "Fifi", undefined, undefined, undefined],
  "Wed":   [undefined, undefined, undefined, "Spike", undefined, undefined],
  "Thurs": [undefined, undefined, undefined, undefined, undefined]
};

function createTimetable(schedule) {
  let table = [], day = '', shortenedDay = '';
  
  let firstRow = [,'9am', '10am', '11am', '12am', '1pm', '2pm'];
  
  table.push(firstRow);
  
  for (let day in schedule) {
    shortenedDay = day.slice(0,3);
    hours = [shortenedDay, ...schedule[day]];
    table.push(hours);
  }

  return table;
}

function printTimetable(timetable) {
  for (let row of timetable) {
    console.log(row.toString());
  }
  
  console.table(timetable)
}

printTimetable( createTimetable(schedule) );

If you want me to fix and reducing your original code, I would go with something like this:

var schedule = {
  "Mon":   [undefined, "Bowser", undefined, undefined, undefined, "Ladykiller"],
  "Tues":  [undefined, undefined, "Fifi", undefined, undefined, undefined],
  "Wed":   [undefined, undefined, undefined, "Spike", undefined, undefined],
  "Thurs": [undefined, undefined, undefined, undefined, undefined]
}


function printSchedule(schedule) {

  var mySchedule = [
    ["    ", "Mon   ", "Tue   ", "Wed   ", "Thu   "],
    ["9am", "      ", "      ", "      ", "      "],
    ["10am", "      ", "      ", "      ", "      "],
    ["11am", "      ", "      ", "      ", "      "],
    ["12am", "      ", "      ", "      ", "      "],
    ["1pm", "      ", "      ", "      ", "      "],
    ["2pm", "      ", "      ", "      ", "      "]
  ];

  let days = mySchedule[0].map(day => day.trim());

  for (let day in schedule) {
    let shorterDay = day.slice(0, 3);
    let index = days.indexOf(shorterDay);

    for (var i = 0; i < schedule[day].length; i++) {
      if (schedule[day][i] !== undefined) {
        mySchedule[i + 1][index] = schedule[day][i];
      }
    }
  }

  console.log('open the console (F12)');
  console.table(mySchedule)
}

printSchedule(schedule)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1