如何通过将 if/else 块替换为 React 中的其他内容来降低 27 的复杂性
How to reduce complexity of 27 by replacing if/else block to something else in React
我有一个带有表情符号的天气显示,由 if/else 语句调整,但由于复杂度为 27,所以明显有重大延迟,我如何用可以降低复杂度的替代方法替换这种方法?
前端世界的新手,非常感谢任何帮助。
function drawWeather( d ) {
var celcius = Math.round(parseFloat(d.main.temp)-273.15);
var fahrenheit =
Math.round(((parseFloat(d.main.temp)-273.15)*1.8)+32);
var main_description = d.weather[0].main;
var description = d.weather[0].description;
var day_time = isDay()
document.getElementById('location').innerHTML = d.name;
if ( main_description === 'Clear' && day_time == true) {
document.getElementById('temp').innerHTML = fahrenheit +
'°' + ' ☀️';
} else if ( main_description === 'Clear' && day_time == false)
{
document.getElementById('temp').innerHTML = fahrenheit +
'°' + ' ';
} else if( main_description === 'Clouds' ) {
document.getElementById('temp').innerHTML = fahrenheit +
'°' + ' ☁️';
} else if( main_description === 'Drizzle' ) {
document.getElementById('temp').innerHTML = fahrenheit +
'°' + ' ️';
} else if( main_description === 'Rain' ) {
document.getElementById('temp').innerHTML = fahrenheit +
'°' + ' ️';
} else if( main_description === 'Thunderstorm' ) {
document.getElementById('temp').innerHTML = fahrenheit +
'°' + ' ⛈️';
} else if( main_description === 'Snow' ) {
document.getElementById('temp').innerHTML = fahrenheit +
'°' + ' ❄️';
} else if( main_description === 'Fog' ) {
document.getElementById('temp').innerHTML = fahrenheit +
'°' + ' ️';
} else if( main_description === 'Mist' ) {
document.getElementById('temp').innerHTML = fahrenheit +
'°' + ' ️';
} else if( main_description === 'Haze' ) {
document.getElementById('temp').innerHTML = fahrenheit +
'°' + ' ️';
} else if( main_description === 'Tornado' ) {
document.getElementById('temp').innerHTML = fahrenheit +
'°'
+ ' ️';
} else if( main_description === 'Dust' ) {
document.getElementById('temp').innerHTML = fahrenheit +
'°'
+ ' ️';
} else {
document.getElementById('temp').innerHTML = fahrenheit +
'°';
}
}
至少要证明你确实在使用代码,但找不到解决方法
Post 格式化代码以表达您对社区的尊重
这是编程中的“DRY - 不要重复自己”的话题。您可以使用该关键字搜索并学习它。
下面的代码是我能做的但未经测试。
let data = [
{ weather: 'Clear', icon_day: '☀️', icon_night: '' },
{ weather: 'Clouds', icon: '☁️' },
{ weather: 'Drizzle', icon: '️' },
{ weather: 'Rain', icon: '️' },
{ weather: 'Thunderstorm', icon: '⛈️' },
{ weather: 'Snow', icon: '❄️' },
{ weather: 'Fog', icon: '️' },
{ weather: 'Mist', icon: '️' },
{ weather: 'Haze', icon: '️' },
{ weather: 'Tornado', icon: '️' },
{ weather: 'Dust', icon: '️' }
]
let icon = '';
data.forEach(element => {
if (main_description === 'Clear') {
icon = isDay() ? element.icon_day : element.icon_night
} else if (main_description === element.weather) {
icon = element.icon;
}
});
document.getElementById('temp').innerHTML = `${fahrenheit}° ${icon}`;
我有一个带有表情符号的天气显示,由 if/else 语句调整,但由于复杂度为 27,所以明显有重大延迟,我如何用可以降低复杂度的替代方法替换这种方法?
前端世界的新手,非常感谢任何帮助。
function drawWeather( d ) {
var celcius = Math.round(parseFloat(d.main.temp)-273.15);
var fahrenheit =
Math.round(((parseFloat(d.main.temp)-273.15)*1.8)+32);
var main_description = d.weather[0].main;
var description = d.weather[0].description;
var day_time = isDay()
document.getElementById('location').innerHTML = d.name;
if ( main_description === 'Clear' && day_time == true) {
document.getElementById('temp').innerHTML = fahrenheit +
'°' + ' ☀️';
} else if ( main_description === 'Clear' && day_time == false)
{
document.getElementById('temp').innerHTML = fahrenheit +
'°' + ' ';
} else if( main_description === 'Clouds' ) {
document.getElementById('temp').innerHTML = fahrenheit +
'°' + ' ☁️';
} else if( main_description === 'Drizzle' ) {
document.getElementById('temp').innerHTML = fahrenheit +
'°' + ' ️';
} else if( main_description === 'Rain' ) {
document.getElementById('temp').innerHTML = fahrenheit +
'°' + ' ️';
} else if( main_description === 'Thunderstorm' ) {
document.getElementById('temp').innerHTML = fahrenheit +
'°' + ' ⛈️';
} else if( main_description === 'Snow' ) {
document.getElementById('temp').innerHTML = fahrenheit +
'°' + ' ❄️';
} else if( main_description === 'Fog' ) {
document.getElementById('temp').innerHTML = fahrenheit +
'°' + ' ️';
} else if( main_description === 'Mist' ) {
document.getElementById('temp').innerHTML = fahrenheit +
'°' + ' ️';
} else if( main_description === 'Haze' ) {
document.getElementById('temp').innerHTML = fahrenheit +
'°' + ' ️';
} else if( main_description === 'Tornado' ) {
document.getElementById('temp').innerHTML = fahrenheit +
'°'
+ ' ️';
} else if( main_description === 'Dust' ) {
document.getElementById('temp').innerHTML = fahrenheit +
'°'
+ ' ️';
} else {
document.getElementById('temp').innerHTML = fahrenheit +
'°';
}
}
至少要证明你确实在使用代码,但找不到解决方法
Post 格式化代码以表达您对社区的尊重
这是编程中的“DRY - 不要重复自己”的话题。您可以使用该关键字搜索并学习它。
下面的代码是我能做的但未经测试。
let data = [
{ weather: 'Clear', icon_day: '☀️', icon_night: '' },
{ weather: 'Clouds', icon: '☁️' },
{ weather: 'Drizzle', icon: '️' },
{ weather: 'Rain', icon: '️' },
{ weather: 'Thunderstorm', icon: '⛈️' },
{ weather: 'Snow', icon: '❄️' },
{ weather: 'Fog', icon: '️' },
{ weather: 'Mist', icon: '️' },
{ weather: 'Haze', icon: '️' },
{ weather: 'Tornado', icon: '️' },
{ weather: 'Dust', icon: '️' }
]
let icon = '';
data.forEach(element => {
if (main_description === 'Clear') {
icon = isDay() ? element.icon_day : element.icon_night
} else if (main_description === element.weather) {
icon = element.icon;
}
});
document.getElementById('temp').innerHTML = `${fahrenheit}° ${icon}`;