使用变量访问对象的值

时间:2014-10-27 13:06:41

标签: javascript

function sort_days() {
    var day_of_week = new Date().getDay();
    var list = ["sunday","monday","tuesday","wednesday","thursday","friday","saturday"];
    var sorted_list = list.slice(day_of_week).concat(list.slice(0,day_of_week));
    return sorted_list;
}

var newVal = {"vendor_d":"3","delivery_possible":1,"monday":0,"monday_opening_closing":0,"tuesday":1,"tuesday_opening_closing":"10:13 - 14:30","wednesday":1,"wednesday_opening_closing":"09:30 - 12:01 & 13:01 - 17:10","thursday":1,"thursday_opening_closing":"09:40 - 12:10 & 13:10 - 18:18","friday":0,"friday_opening_closing":0,"saturday":1,"saturday_opening_closing":"05:06 - 13:15 & 14:10 - 20:12","sunday":1,"sunday_opening_closing":"06:20 - 14:15 & 15:15 - 22:20"}
var days = sort_days();

for (var i = 0; i < days.length; i++) {
  console.log(days[i]);
  console.log ( newVal.days[i] ); // here I wanna get newVal.monday value
};

这里我希望获得newVal.days[i]值。我不太确定我做得对。但当我把newVal.monday给它给我正确的价值时。

1 个答案:

答案 0 :(得分:3)

newVal没有days属性,这是您尝试访问的属性。你的意思是说:

 console.log ( newVal[ days[i] ] );