我有这个普通的对象,我已经从weather API返回了代码,所以根据代码我想显示不同的HTML。代码应该与codeArrays中的一个匹配,并且根据匹配我应该具有相应的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]
}
};
答案 0 :(得分:0)
这是我得到的地方......
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;
}
它有效...很抱歉打扰并感谢在Plain Object和JSON之间明确