-1

I have this plain Object, and I have returned code from weather API, so depending on the code i want to display different HTML. The code should match one of the codeArrays and depending on the match I should have the appropriate html

var WeatherIcons = {
    sunShower: {
        html: '<div class="icon sun-shower"></div>',
        codes: [1009, 1150, 1153, 1240, 1249]
    },
    thunderStorm: {
        html: '<div class="icon thunder-storm"></div>',
        codes: [1087, 1117, 1147, 1273, 1276, 1279]
    },
    cloudy: {
        html: '<div class="icon cloudy"></div>',
        codes: [1006, 1030, 1135]
    },
    snowing: {
        html: '<div class="icon snowing"></div>',
        codes: [1066, 1114, 1210, 1213, 1219, 1222, 1225, 1255, 1258, 1261, 1264, 1282]
    },
    sunny: {
        html: '<div class="icon sunny"></div>',
        codes: [1000, 1003, 1216]
    },
    rainy: {
        html: '<div class="icon rainy"></div>',
        codes: [1063, 1069, 1072, 1168, 1171, 1180, 1183, 1186, 1189, 1192, 1195, 1198, 1201, 1204, 1237, 1243, 1246, 1252]
    }
};
Sensei
  • 1
  • 4
  • 2
    That's no "json array" but a plain old object -> [What is the difference between JSON and Object Literal Notation?](https://stackoverflow.com/questions/2904131/what-is-the-difference-between-json-and-object-literal-notation) – Andreas Mar 20 '17 at 12:01
  • @Andreas Sorry, it is plain old object, I can convert it in JSON, any will work – Sensei Mar 20 '17 at 12:03
  • @T.J.Crowder Thanks I get it, Edited the question – Sensei Mar 20 '17 at 12:08
  • Don't know how to Title the question... @T.J.Crowder Cr – Sensei Mar 20 '17 at 12:24
  • Are you saying the weather API will return a code like 1009 or 1147, i.e., one of the codes in one of your arrays, and you want to figure out which object contains that code? – nnnnnn Mar 20 '17 at 12:52
  • @nnnnnn yes, that was the point, but already got the solution – Sensei Mar 20 '17 at 13:35

1 Answers1

0

Here is where I got...

var resolveWeatherIcon = function (Condition) {
    var iconHtml = '';

    $j.each(WeatherIcons, function (key, val) {

        if (val.codes.indexOf(Condition.code) >= 0) {
            iconHtml = WeatherIcons[key].html;
            return false;
        };
    })

    return iconHtml;

}

it works... Sorry for bothering and thanks for making it clear between Plain Object and JSON

Sensei
  • 1
  • 4