简体   繁体   中英

Reduce JavaScript iteration

Now I'm developing timeline program with javascript. When I click each class, it should be colored to timeline with 2 blocks because each classes are 2hours classes.

each timeline table has id(ex. monday first class has ="mon1" and firday second class has '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. 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:

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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