1

I am working on an application where I am fetching the weather information from OpenWeatherMaps API and then plotting it on graphs using FlotJS.

The data is coming from:

http://api.openweathermap.org/data/2.5/forecast?lat=39.77476949&lon=-100.1953125

Where the lat, lon are dynamically generated. You can go to the link to get an idea of what kind of JSON response is sent back.

I am trying to construct the arrays first in order to pass them into the data object while plotting the graph like so:

var temp = [];
var humidity = [];
var rain = [];

$.each(response.list, function(i, item){
    if(moment(item.dt, 'X').isSame(moment(), 'day')){
        temp.push([moment(item.dt, 'X').valueOf(), parseFloat(item.main.temp)]);
        humidity.push([moment(item.dt, 'X').valueOf(), parseFloat(item.main.humidity)]);
        rain.push([moment(item.dt, 'X').valueOf(), parseFloat(item.rain.3h)]);
    }
});

temp and humidity were working fine. I cannot add precipitation. The precipitation values are in the rain object in 3h and when I try to push it into the array, I get a console error saying:

SyntaxError: identifier starts immediately after numeric literal

How to go around this problem?

Rohan
  • 13,308
  • 21
  • 81
  • 154

1 Answers1

5

3h isn't a valid symbol, it's a numeric literal followed by an "h"; parser go boom.

You need item.rain['3h'] instead.

Dave Newton
  • 158,873
  • 26
  • 254
  • 302
  • Wow. Why couldn't I think of that. Sometimes it is simplest of things that confuse me. Thanks. I will accept it as soon as it lets me. – Rohan Jun 23 '15 at 10:41