0

In the weather array I am using, rain only shows up if it's raining. How do I do I skip a field if it is not in the array and move on?

$.ajax({
  type: 'GET',
  url: "http://api.openweathermap.org/data/2.5/forecast?lat="+latitude+"&lon="+longitude+"&units=imperial&APPID=removed",
  success: function(response) {
    icon = "wi-day-"+response.list[0].weather[0].main.toLowerCase();
    $('#city').html(response.city.name);
    $('#country').html(response.city.country);
    $('#current_temp').html(Math.round(response.list[0].main.temp)+' ºF');
    $('#weather_description').html(response.list[0].weather[0].description);
    $('#weather').html(response.list[0].weather[0].main);
    $('#max_temp').html(Math.round(response.list[0].main.temp_max)+' F');
    $('#min_temp').html(Math.round(response.list[0].main.temp_min)+' F');
    $('#humidity').html(response.list[0].main.humidity+' %');
    $('#rain_volume').html(response.list[0].rain[3h]+'"');
    $('#wind_speed').html(response.list[0].wind.speed+'MPH');

       console.log(response);
  }

});

The line I am talking about

 $('#rain_volume').html(response.list[0].rain[3h]+'"');
mhodges
  • 10,938
  • 2
  • 28
  • 46
user163169
  • 135
  • 2
  • 9
  • if(response.list[0].hasOwnProperty("rain")) $('#rain_volume').html(response.list[0].rain[3h]+'"'); – juvian Jan 26 '17 at 17:03
  • I would say `(response.list[0].rain[3h] || "0")+'"'` That way, if `rain[3h]` is falsey, it will fall back to `"0"` and continue on. The issue that you're getting is trying to do string concatenation on a falsey or undefined value – mhodges Jan 26 '17 at 17:07
  • 1
    use `response.list[0].rain["3h"]` instead in case you are sure a rain object in your response. otherwise try to use `response.list[0].rain && response.list[0].rain["3h"]` – Tolgahan Albayrak Jan 26 '17 at 17:10
  • @TolgahanAlbayrak Agreed, I was going to clarify that, but you beat me to it ;) – mhodges Jan 26 '17 at 17:14
  • Side note -- you should most definitely save off `response.list[0]` into a variable so you don't have to keep repeating that over and over. – mhodges Jan 26 '17 at 17:15
  • @mhodges i have an idea. see the answer – Tolgahan Albayrak Jan 26 '17 at 17:39

3 Answers3

1

This is just an example to avoid all if checks. Personally I would not use it. But just an idea

function always(obj){
  return new Proxy(obj, {
    get(target, prop) {
      if(target.hasOwnProperty(prop) && typeof target[prop] !== 'object'){
        return target[prop];
      }
      return always(target[prop] || {[Symbol.toPrimitive]:() => ''});
    }
  });
}
let response = {}; // some response
response = always(response);

// this line will not throw any error and will print an empty line
console.log(response.list[0].rain["3h"]+"");

response = {list:[{rain:{"3h":"test"}}]}; // some response
response = always(response);

// this line should print 'test'
console.log(response.list[0].rain["3h"]+"");
Tolgahan Albayrak
  • 3,118
  • 1
  • 25
  • 28
0

It's not quite correct to say that the rain element isn't in the array. You have an array of objects, and some of those objects may have a rain property and some may not. With that in mind, the answer is here:

check if object property exists - using a variable

Community
  • 1
  • 1
Chris Long
  • 1,299
  • 7
  • 15
0

If I understand you correctly, you should be able to just skip over it using an IF statement:

if (response.list[0].rain) {
    $('#rain_volume').html(response.list[0].rain[3h]+'"');
}
mjgreen145
  • 155
  • 9
  • There should be an else condition, because if there is already data loaded from a previous location, and the user requests data for a new location, this code will leave the old rain data on the screen. – mhodges Jan 26 '17 at 17:18
  • `rain[3h]` is a syntax error, it should be `rain["3h"]`. Also, if `rain = {}`, this spits out `undefined"`. Why is this the accepted answer? – mhodges Jan 26 '17 at 18:29
  • The question did specify skipping if `rain` was not in the array, so this works in that case. – mjgreen145 Jan 26 '17 at 20:41
  • First off: `list[0]` is not an array - it is an object. Secondly, `rain[3h]` is still a syntax error. And thirdly, it doesn't address the problem that if location 1 had rain data and location 2 does not, this does not reset the rain data and will show location 1's data for location 2. – mhodges Jan 26 '17 at 20:45