简体   繁体   English

减少JavaScript迭代

[英]Reduce JavaScript iteration

Now I'm developing timeline program with javascript. 现在,我正在使用javascript开发时间轴程序。 When I click each class, it should be colored to timeline with 2 blocks because each classes are 2hours classes. 当我单击每个班级时,由于每个班级均为2小时课程,因此应将其着色为带有2个块的时间轴。

each timeline table has id(ex. monday first class has ="mon1" and firday second class has 'fri2' 每个时间轴表都有id(例如,星期一的第一堂课有=“ mon1”,而生日的第二堂课有'fri2'

Here's my code. 这是我的代码。 and It works well. 而且效果很好。 but I want to know how can I reduce the code. 但我想知道如何减少代码。

let timelineColor = getRamdomColor();
let clickDay;
let clickDay1;

for (i = 0; i < dayArray.length; i++) {
    if (dayArray[i] === 'monday') {
        clickDay = 'mon' + timeArray[i];
        let table = document.getElementById(clickDay);
        table.style.backgroundColor = timelineColor;
        clickDay1 = 'mon' + (Number(timeArray[i]) + 1);
        console.log(clickDay1);
        let table1 = document.getElementById(clickDay1);
        table1.style.backgroundColor = timelineColor;
    } else if (dayArray[i] === 'tuesday') {
        clickDay = 'tue' + timeArray[i];
        let table = document.getElementById(clickDay);
        table.style.backgroundColor = timelineColor;
        clickDay1 = 'tue' + (Number(timeArray[i]) + 1);
        let table1 = document.getElementById(clickDay1);
        table1.style.backgroundColor = timelineColor;
    } else if (dayArray[i] === 'wednesday') {
        clickDay = 'wed' + timeArray[i];
        let table = document.getElementById(clickDay);
        table.style.backgroundColor = timelineColor;
        clickDay1 = 'wed' + (Number(timeArray[i]) + 1);
        let table1 = document.getElementById(clickDay1);
        table1.style.backgroundColor = timelineColor;
    } else if (dayArray[i] === 'thursday') {
        clickDay = 'tur' + timeArray[i];
        let table = document.getElementById(clickDay);
        table.style.backgroundColor = timelineColor;
        clickDay1 = 'tur' + (Number(timeArray[i]) + 1);
        let table1 = document.getElementById(clickDay1);
        table1.style.backgroundColor = timelineColor;
    } else if (dayArray[i] === 'friday') {
        clickDay = 'fri' + timeArray[i];
        let table = document.getElementById(clickDay);
        table.style.backgroundColor = timelineColor;
        clickDay1 = 'fri' + (Number(timeArray[i]) + 1);
        let table1 = document.getElementById(clickDay1);
        table1.style.backgroundColor = timelineColor;
    }
}

时间线

You don't really need the if -statements at all. 您实际上根本不需要if语句。 Set up a map that relates your full day name to the short day names, and look it up: 设置一个将您的全天名称与短日名称相关联的地图,然后进行查找:

let timelineColor = getRamdomColor();
let clickDay;
let clickDay1;

var dayMap = {
    'monday'   : 'mon',
    'tuesday'  : 'tue',
    'wednesday': 'wed',
    'thursday' : 'tur',
    'friday'   : 'fri'
};

for (var i = 0; i < dayArray.length; i++) {
    let shortDayName = dayMap[dayArray[i]];
    clickDay = shortDayName + timeArray[i];
    let table = document.getElementById(clickDay);
    table.style.backgroundColor = timelineColor;
    clickDay1 = shortDayName + (Number(timeArray[i]) + 1);
    console.log(clickDay1);
    let table1 = document.getElementById(clickDay1);
    table1.style.backgroundColor = timelineColor;
}

You could utilise switch statement instead of multiple if-elseif statement: 您可以使用switch语句而不是多个if-elseif语句:

let timelineColor = getRamdomColor();

for (i = 0; i < dayArray.length; i++){
  let day;

  switch (dayArray[i]){
    case 'monday': day = 'mon'; break;
    case 'tuesday': day = 'tue'; break;
    case 'wednesday': day = 'wed'; break;
    case 'thursday': day = 'tur'; break;
    case 'friday': day = 'fri'; break;
  }

  // I moved these variables into the loop too
  let clickDay = day + timeArray[i];
  let clickDay1 = day + (Number(timeArray[i]) + 1);

  let table = document.getElementById(clickDay);
  let table1 = document.getElementById(clickDay1);

  table.style.backgroundColor = timelineColor;
  table1.style.backgroundColor = timelineColor;
}

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

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