我有一个任务要做,我有一个数组的天。我将不得不打印,如果数组包括特定的天。数组可以有任何天在它的用户将决定他们想打印的天。
callDays(){
const days: string[]=['Sunday','Monday','Wednesday','Thursday','Friday','Saturday'];
if(days.includes('Sunday')){
console.log('Sunday is included');
}
if(days.includes('Monday')){
console.log('Monday is included');
}
if(days.includes('Tuesday')){
console.log('Tuesday is included');
}
if(days.includes('Wednesday')){
console.log('Wednesday is included');
}
if(days.includes('Thursday')){
console.log('Thursday is included');
}
if(days.includes('Friday')){
console.log('Friday is included');
}
if(days.includes('Saturday')){
console.log('Saturday is included');
}
}
在上面的例子中,星期二不应该被打印出来,因为它在数组中是不可用的,为了完成这个任务,我不得不使用这么多的if语句,有没有一种方法可以不用这么多的if语句来完成它,谢谢。
1条答案
按热度按时间h5qlskok1#
您可能需要这样的内容:
稍微复杂但可能更快的实现是:
编辑:用于调用基于所提供数组中休息日的函数