将巨大的 if 语句变量重构为数组

我想将这些变量重构为数组,因为它看起来很臃肿,但不知道这样做的最佳做法,因为它们太多了。请看一看,让我知道你认为我应该如何去做。



VS_group = "group";


if (VS_group == "group") {

                    V1_IC11 = getAnswerCode('INBOARD_111_COUNT');

                    V1_IC12 = getAnswerCode('INBOARD_112_COUNT');

                    V1_IC13 = getAnswerCode('INBOARD_113_COUNT');

                    V1_IC14 = getAnswerCode('INBOARD_114_COUNT');

                    V1_IC15 = getAnswerCode('INBOARD_115_COUNT');

                    V1_IC16 = getAnswerCode('INBOARD_116_COUNT');

                    V1_IC17 = getAnswerCode('INBOARD_117_COUNT');

                    V1_IC18 = getAnswerCode('INBOARD_118_COUNT');

                    V1_IC19 = getAnswerCode('INBOARD_119_COUNT');

                    V1_IC20 = getAnswerCode('INBOARD_120_COUNT');

                    V1_IC31 = getAnswerCode('INBOARD_131_COUNT');

                    V1_IC32 = getAnswerCode('INBOARD_132_COUNT');

                    V1_IC33 = getAnswerCode('INBOARD_133_COUNT');

                    V1_IC34 = getAnswerCode('INBOARD_134_COUNT');

                    V1_IC35 = getAnswerCode('INBOARD_135_COUNT');

                    V1_IC36 = getAnswerCode('INBOARD_136_COUNT');

                    V1_IC37 = getAnswerCode('INBOARD_137_COUNT');

                    V1_IC38 = getAnswerCode('INBOARD_138_COUNT');

                    V1_IC39 = getAnswerCode('INBOARD_139_COUNT');

                    V1_IC40 = getAnswerCode('INBOARD_140_COUNT');

                    V2_IC11 = getAnswerCode('INBOARD_211_COUNT');

                }


哆啦的时光机
浏览 104回答 4
4回答

翻过高山走不出你

只需记下使用两次的两个范围即可。然后循环访问这些范围并将结果保存到对象中以供参考。const lower = [11, 31];const upper = [20, 40];const vs = [1, 2];const vsGroup = {};for (let vn = 0; vn < vs.length; vn++) {&nbsp; for (let i = 0; i < lower.length; i++) {&nbsp; &nbsp; for (let j = lower[i]; j <= upper[i]; j++) {&nbsp; &nbsp; &nbsp; vsGroup["V" + vs[vn] + "_IC" + j] = 'getAnswerCode(INBOARD_' + vs[vn] + '' + j + '_COUNT)';&nbsp; &nbsp; }&nbsp; }}console.log(vsGroup);(getAnswerCode 保留为字符串,只是为了显示调用的外观)

暮色呼如

如果您创建一个代码数组,则可以将其映射到答案,如下所示:const codesINeed = ["INBOARD_111_COUNT", "INBOARD_112_COUNT"];// (etc)const answerCodes = codesINeed.map(getAnswerCode);// answerCodes is an array of answer codes with the same order as codesINeed

交互式爱情

你可以像这样创建一个数组:let vs_group =&nbsp;[&nbsp; {&nbsp; &nbsp; &nbsp;"name": "V1_IC11",&nbsp; &nbsp; &nbsp;"value": "INBOARD_111_COUNT"&nbsp; },&nbsp; {&nbsp; &nbsp; &nbsp;"name": "V1_IC12",&nbsp; &nbsp; &nbsp;"value": "INBOARD_112_COUNT"&nbsp; }&nbsp;&nbsp; ...]

喵喵时光机

您可以将所有代码放在一个数组中,并对其进行映射,以为每个代码创建一个对象,并具有代码和相关答案。这样,您就不会忘记哪个答案与哪个代码一起使用。// sample function for demofunction getAnswerCode(x) {&nbsp; return "answer for " + x;}const codes = ['INBOARD_111_COUNT','INBOARD_112_COUNT','INBOARD_113_COUNT'] // and the restconst answers = codes.map(code => { return { code, answer: getAnswerCode(code)&nbsp; }})console.log(answers);// You can later get what you need using filter:const answerFor112 = answers.filter(a => a.code === 'INBOARD_112_COUNT')[0].answer;console.log("Sample answer requested: " + answerFor112);
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript