0

I have a for loop in JS when I set a breakpoint inside the for a loop the index set it has a value in one usage but the other says undefined.

I have a breakpoint set at the line that starts $("#send_hour option") If I mouseover index in the for(index in availablehours) it shows 09. If I mouseover index at the end index); it also shows 09. But if I mouseover index here availablehours[index] it shows as undefined?

var availablehours = {
    "09" : '9AM',
    "10" : '10AM',
    "11" : '11AM',
    "12" : 'Noon',
    "13" : '1PM',
    "14" : '2PM',
    "15" : '3PM',
    "16" : '4PM',
    "17" : '5PM',
    "18" : '6PM',
    "19" : '7PM'
};

How is this possible?

function refreshTheSendHoursWithAvailableHours(){
    console.log( "refreshTheSendHoursWithAvailableHours just called.");
      for(index in availablehours) {
       $("#send_hour option")[ $("#send_hour option").length] = new Option(availablehours[index], index);
      }
  }
Hyyan Abo Fakher
  • 3,497
  • 3
  • 21
  • 35
jdog
  • 10,351
  • 29
  • 90
  • 165
  • 1
    The value of `index` is `09`. The value of the array element at that index `availablehours[index]` is undefined. – Matt Sep 11 '18 at 03:52
  • Ok, but I am mousing over index not what availablehours[index] is. Also, there is something at position 09 in available hours. Also the code is suppose to stuff all the items in availablehours in as options to a select. The for loop code above isn't creating them. – jdog Sep 11 '18 at 03:57
  • have not totally read the question, but debugger tool would have some bug, the most reliable way is access it through code (e.g. print to console). – apple apple Sep 11 '18 at 04:02

1 Answers1

0

Doing a mouseover on index and mouseover on availablehours[index] will give two different values.

index : gives value of current index

availablehours[index] : gives value of element stored in array at that index

pixlboy
  • 1,452
  • 13
  • 30