简体   繁体   中英

How to refactor these bloated conditional statements into an Array

I want to refactor this code into an array to simplify it and make it less bloated. Ideally I'd like to put it into an array because I think there's too many conditional statements going on here. Can anyone help me somehow refactor this code into an Array?

//Reset Connect-n-Go Options when not Connect-n-Go
    if ((VS_SELQ == "WIRING") && (VS_SELA != "CNG")) {
        //RESET Connect-n-Go Presets
            if (getAnswerCode('CNG_PRESETS') !== "") {
                reset('CNG_PRESETS');
            }
        //RESET Warning Pattern #1
            if (getAnswerCode('PATTERN_WARN_1') !== "") {
                reset('PATTERN_WARN_1');
            }
        //RESET Warning Pattern #2
            if (getAnswerCode('PATTERN_WARN_2') !== "") {
                reset('PATTERN_WARN_2');
            }
        //RESET Arrow Pattern
            if (getAnswerCode('PATTERN_ARROW') !== "") {
                reset('PATTERN_ARROW');
            }
        //RESET Fascia Quantity
            if (getAnswerCode('FASCIA_QTY') !== "") {
                reset('FASCIA_QTY');
            }
        //RESET Extra Harnesses
            if (getAnswerCode('EXT_HARN') !== "") {
                reset('EXT_HARN');
            }
        //RESET Front Fascia Warning Color #1
            if (getAnswerCode('FRONT_FASCIA_COLOR_WARN_1') !== "") {
                reset('FRONT_FASCIA_COLOR_WARN_1');
            }
        //RESET Front Fascia Warning Color #2
            if (getAnswerCode('FRONT_FASCIA_COLOR_WARN_2') !== "") {
                reset('FRONT_FASCIA_COLOR_WARN_2');
            }
        //RESET Front Fascia Worklight Color
            if (getAnswerCode('FRONT_FASCIA_COLOR_WORK') !== "") {
                reset('FRONT_FASCIA_COLOR_WORK');
            }
        //RESET Rear Fascia Warning Color #1
            if (getAnswerCode('REAR_FASCIA_COLOR_WARN_1') !== "") {
                reset('REAR_FASCIA_COLOR_WARN_1');
            }
        //RESET Rear Fascia Warning Color #2
            if (getAnswerCode('REAR_FASCIA_COLOR_WARN_2') !== "") {
                reset('REAR_FASCIA_COLOR_WARN_2');
            }
        //RESET Rear Fascia Worklight Color
            if (getAnswerCode('REAR_FASCIA_COLOR_WORK') !== "") {
                reset('REAR_FASCIA_COLOR_WORK');
            }
    }

Just make an array of those strings you pass to getAnswerCode and to reset , then iterate over the array:

const answerCodes = [
  'CNG_PRESETS',
  'PATTERN_WARN_1',
  'PATTERN_WARN_2',
  // ...
];

if ((VS_SELQ == "WIRING") && (VS_SELA != "CNG")) {
  for (const answerCode of answerCodes) {
    if (getAnswerCode(answerCode) !== "") {
      reset(answerCode);
    }
  }
}

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