简体   繁体   English

遍历数组匹配变量并分配条件以执行

[英]Loop through array match variable and assign condition to execute

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). 因此,我使用了免费的预测API,并采用了ICON的“对象”值(如果下雨,下雪等情况,图标会在不同的时间显示)。 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. 我已经创建了一个数组,其中包含有关该对象值的所有可能选项:根据该预报API的文档,显示“ icon”,并且我在Img文件夹中准备了所有图像。 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. 所以我的想法是使用所有可能的天气条件选项循环遍历此数组,然后,如果与我的ICON变量匹配(根据我的API一直在变化),我希望能够为CURRENT分配适当的图像小麦状况:降雨-分配Rain.png,或者如果正在下雪-分配Snow.png,依此类推。

Here is my code until now but I have problem with the matching my array with this ICON variable output. 到目前为止,这是我的代码,但是我无法将数组与此ICON变量输出进行匹配。 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');
        }
    }   

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');
}

Assuming dayOneIconTokyo is an array 假设dayOneIconTokyo是一个数组

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM