1

Currently I am building web app which is able to show the weather in real time but I stuck on one of my ideas. So I am using free forecast API and I am taking the Object value for ICON (icon that says in different time if it's raining, snowing and so on). I have created array with all possible options about this object value: 'icon' according the documentation of that forecast API and I have prepared all Images in my Img folder. So my idea is to loop through this array with all possible options for weather condition then if there is a match with my ICON variable which is changing all the time according to my API, I want to be able to assign the proper image for the CURRENT wheather condition: Rain - Assign Rain.png or if it is Snowing - Assign Snow.png and so on.

Here is my code until now but I have problem with the matching my array with this ICON variable output. And I have made also array with all of my images that I would like to assign if there is a match.

var weatherConditions = [
            'Clear',
            'Possible Light Precipitation',
            'Light Precipitation',
            'Precipitation',
            'Drizzle',
            'Possible Drizzle',
            'Possible Light Rain',
            'Light Rain',
            'Rain',
            'Heavy Rain',
            'Possible Light Sleet',
            'Light Sleet',
            'Sleet',
            'Heavy Sleet',
            'Possible Flurries',
            'Flurries',
            'Possible Light Snow',
            'Light Snow',
            'Snow',
            'Heavy Snow',
            'Windy',
            'Dangerously Windy',
            'Foggy',
            'Mostly Cloudy',
            'Overcast',
            'Dry and Breezy',
            'Drizzle and Dangerously Windy'
        ]; 
    var arrayLength = weatherConditions.length;
    for (var i = 0; i < arrayLength; i+=1){
        if ( weatherConditions[i].indexOf(dayOneIconTokyo) ){
            console.log('working');
        } else{
            console.log('not in the array');
        }
    }   
divisionkiller
  • 316
  • 3
  • 13
  • 1
    what is the content of `dayOneIconTokyo`? – Nina Scholz Feb 29 '16 at 14:26
  • Have you considered creating a mapping object? `{"name":"key1", "src":"1.png"}` – Rajesh Feb 29 '16 at 14:26
  • dayOneIconTokyo is my object value that I took from the API which says what is the current weather condition (raining , snowing , cloudy and so on) for the specific city – divisionkiller Feb 29 '16 at 14:28
  • what should be returned if `dayOneIconTokyo` is `Snow`? – Nina Scholz Feb 29 '16 at 14:30
  • I would like to return the image for the current condition - Snowing -> return snow.png and for other conditions same return rain -rain.png and so on – divisionkiller Feb 29 '16 at 14:32
  • Then should you not use `if ( weatherConditions.indexOf(dayOneIconTokyo)>-1 ){ console.log("working")} else{ console.log("Not Working")}` – Rajesh Feb 29 '16 at 14:38
  • You can refer following [Post](http://stackoverflow.com/questions/237104/how-do-i-check-if-an-array-includes-an-object-in-javascript) for more reference – Rajesh Feb 29 '16 at 15:02

2 Answers2

0

No need for looping:

if (~weatherConditions.indexOf(dayOneIconTokyo)) { // returns true if dayOneIconTokyo
                                                   // is in weatherConditions
    console.log('working');
} else{
    console.log('not in the array');
}
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
  • Nina, I may have misunderstood but directly using `array.indexOf(value)` wont be better? – Rajesh Feb 29 '16 at 14:51
  • Just a small query, if OP wanted to do a partial search, then what would be preferred/better: `array.some` or `array.filter`? – Rajesh Feb 29 '16 at 15:07
  • @Rajesh, it depends on the wanted result, if only look up, then `Array#some` is shorter because of short circuiting. if a collection should be returned, than `Array#filter` is a better choice. – Nina Scholz Feb 29 '16 at 15:23
0

Assuming dayOneIconTokyo is an array

for (var i = 0; i < dayOneIconTokyo.length; i++){
    if ( weatherConditions.indexOf(dayOneIconTokyo[i]) ){
        console.log('working');
    } else{
        console.log('not in the array');
    }
}
Johan Kvint
  • 907
  • 1
  • 8
  • 17