0

I am calling an api which returns an object

var rain = data[i].rain;

$.get( "http://api.openweathermap.org/data/2.5/forecast?q=London,uk&mode=json&units=metric", function( data ) {
    var data = data.list;
    for(var i = 0; i < 12; i++){
        var rain = data[i].rain;
        console.log(rain)
    }
});

rain returns the following when I inspect the console:

Object {3h: 0.005}
Object {3h: 0.03}

I expected that rain.3h would have given me access to 0.005 and 0.03, but it doesn't. I get the following error message : Unexpected token ILLEGAL

Is this because it begins with a number? Can anyone advise how to access this property?

Paul Fitzgerald
  • 11,770
  • 4
  • 42
  • 54

1 Answers1

2

Yes it's because it begins with a number.

You can access that like this:

rain['3h']

When you have object properties named with numbers or simbols, use the bracket notation.

pietrovismara
  • 6,102
  • 5
  • 33
  • 45