4 回答
TA贡献1875条经验 获得超3个赞
只需记下使用两次的两个范围即可。然后循环访问这些范围并将结果保存到对象中以供参考。
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 保留为字符串,只是为了显示调用的外观)
TA贡献1853条经验 获得超9个赞
如果您创建一个代码数组,则可以将其映射到答案,如下所示:
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
TA贡献1712条经验 获得超3个赞
你可以像这样创建一个数组:
let vs_group =
[
{
"name": "V1_IC11",
"value": "INBOARD_111_COUNT"
},
{
"name": "V1_IC12",
"value": "INBOARD_112_COUNT"
}
...
]
TA贡献1846条经验 获得超7个赞
您可以将所有代码放在一个数组中,并对其进行映射,以为每个代码创建一个对象,并具有代码和相关答案。这样,您就不会忘记哪个答案与哪个代码一起使用。
// 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);
添加回答
举报