[Javascript] [Noob]访问对象内的值

时间:2016-11-14 20:17:45

标签: javascript arrays object printing 2d

我正在尝试打印通过对象输入给我的日程表的二维数组。当它打印时,它没有给我我想要的值。在深入了解对象值时,不知道我做错了什么?

下面给我的时间表:

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", "      ", "      ", "      ", "      "]];


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

当我真正想要对象内部的字符串时,当前输出给了我“M”“o”“T”之类的东西。谢谢!

0 个答案:

没有答案