You are reading a single comment by @Robin and its replies. Click here to read the full conversation.
  • I've stumbled across a problem that I can't get my head around and am not sure what to search for to learn how to resolve it. Can anyone help name a technique or method which I can read up on?

    I'm trying to access a propety of an object which changes depending on the output of a function. This is a simplified example:

    var days = {
      Monday: {hour: 6, minute: 30},
      Tuesday: {hour: 7, minute: 0},
    };
    
    var today = function() {
      return "Tuesday" // simplified for this example - not sure if return is best approach
    };
    
    console.log(days[today].hour);
    

    Espruino says:

    Uncaught Error: Cannot read property 'hour' of undefined
     at line 1 col 24
    console.log(days[today].hour);
                           ^
    

    Do I need to get today to return a string instead? Any help would be much appreciated.

    This is a situation where I don't know enough to help myself :)

  • I restructured my data in "days" a few times and lost understanding of what I was dealing with. I thought that it was an object as I couldn't reference its contents with an index e.g

    console.log(days[2].hour);
    

    Firefox console says:

    TypeError: days[2] is undefined
    

    If it's an array after all, then I'll do some more reading about arrays. Thanks

About

Avatar for Robin @Robin started