Reputation: 117
I would like to refactor these variables into an array because it seems bloated but dont know that best course of action to do so since there are so many of them. Please take a look and let me know how you think I should go about this.
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');
V2_IC12 = getAnswerCode('INBOARD_212_COUNT');
V2_IC13 = getAnswerCode('INBOARD_213_COUNT');
V2_IC14 = getAnswerCode('INBOARD_214_COUNT');
V2_IC15 = getAnswerCode('INBOARD_215_COUNT');
V2_IC16 = getAnswerCode('INBOARD_216_COUNT');
V2_IC17 = getAnswerCode('INBOARD_217_COUNT');
V2_IC18 = getAnswerCode('INBOARD_218_COUNT');
V2_IC19 = getAnswerCode('INBOARD_219_COUNT');
V2_IC20 = getAnswerCode('INBOARD_220_COUNT');
V2_IC31 = getAnswerCode('INBOARD_231_COUNT');
V2_IC32 = getAnswerCode('INBOARD_232_COUNT');
V2_IC33 = getAnswerCode('INBOARD_233_COUNT');
V2_IC34 = getAnswerCode('INBOARD_234_COUNT');
V2_IC35 = getAnswerCode('INBOARD_235_COUNT');
V2_IC36 = getAnswerCode('INBOARD_236_COUNT');
V2_IC37 = getAnswerCode('INBOARD_237_COUNT');
V2_IC38 = getAnswerCode('INBOARD_238_COUNT');
V2_IC39 = getAnswerCode('INBOARD_239_COUNT');
V2_IC40 = getAnswerCode('INBOARD_240_COUNT');
}
Upvotes: 2
Views: 80
Reputation: 816
You can put all the codes in an array and map it to create an object for each of them, having the code and the related answer. That way you don't lose track of which answer goes with which code.
// sample function for demo
function getAnswerCode(x) {
return "answer for " + x;
}
const codes = ['INBOARD_111_COUNT',
'INBOARD_112_COUNT',
'INBOARD_113_COUNT'] // and the rest
const answers = codes.map(code => { return { code, answer: getAnswerCode(code) }})
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);
Upvotes: 1
Reputation: 82297
Just make note of the two ranges, used twice. Then iterate those ranges and save the result into an object for reference.
const lower = [11, 31];
const upper = [20, 40];
const vs = [1, 2];
const vsGroup = {};
for (let vn = 0; vn < vs.length; vn++) {
for (let i = 0; i < lower.length; i++) {
for (let j = lower[i]; j <= upper[i]; j++) {
vsGroup["V" + vs[vn] + "_IC" + j] = 'getAnswerCode(INBOARD_' + vs[vn] + '' + j + '_COUNT)';
}
}
}
console.log(vsGroup);
(getAnswerCode left as a string just to show what the call would look like)
Upvotes: -1
Reputation: 22246
If you make an array of codes, you can map it to the answers like this:
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
Upvotes: 3
Reputation: 4692
You could make an array like this:
let vs_group =
[
{
"name": "V1_IC11",
"value": "INBOARD_111_COUNT"
},
{
"name": "V1_IC12",
"value": "INBOARD_112_COUNT"
}
...
]
Upvotes: 1