首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >简化多个相似的If语句(JAVASCRIPT)

简化多个相似的If语句(JAVASCRIPT)
EN

Stack Overflow用户
提问于 2021-03-12 21:36:44
回答 2查看 30关注 0票数 1

我正在为一个adobe acrobat窗体编写代码,我想添加以下代码:

代码语言:javascript
复制
var total =6301
var warranty
if (0 < total && total <= 3300){warranty = 194.25}
else if (3300 < total && total <= 4000){warranty = 197.5}
else if (4000 < total && total <= 5000){warranty = 202.15}
else if (5000 < total && total <= 6000){warranty = 206.75}
else if (6000 < total && total <= 7000){warranty = 211.45}
else if (7000 < total && total <= 8000){warranty = 216.1}
else if (8000 < total && total <= 9000){warranty = 220.75}
else if (9000 < total && total <= 10000){warranty = 225.4}
else if (10000 < total && total <= 11000){warranty = 230.1}
else if (11000 < total && total <= 12000){warranty = 234.75}
else if (12000 < total && total <= 13000){warranty = 239.4}
else if (13000 < total && total <= 14000){warranty = 243.95}
else if (14000 < total && total <= 15000){warranty = 248.7}
else if (15000 < total && total <= 16000){warranty = 253.3}
else if (16000 < total && total <= 17000){warranty = 258}
else if (17000 < total && total <= 18000){warranty = 262.65}
else if (18000 < total && total <= 19000){warranty = 267.3}
else if (19000 < total && total <= 20000){warranty = 271.95}
else if (20000 < total && total <= 21000){warranty = 276.6}
else if (21000 < total && total <= 22000){warranty = 281.3}
else if (22000 < total && total <= 23000){warranty = 285.9}
else if (23000 < total && total <= 24000){warranty = 290.5}
else if (24000 < total && total <= 25000){warranty = 295.25}
else if (25000 < total && total <= 26000){warranty = 299.85}
else if (26000 < total && total <= 27000){warranty = 304.55}
else if (27000 < total && total <= 28000){warranty = 309.15}
else if (28000 < total && total <= 29000){warranty = 313.85}
else {warranty = 999999}

我希望这样做的重复性要少得多,因为我有更多类似于上面的条件。

提前感谢您的帮助!!

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2021-03-12 21:53:42

这种方法的特点是具有值对的数组,该数组适用于任何值。

代码语言:javascript
复制
const
    getValue = total => [
        [3300, 194.25], [4000, 197.5], [5000, 202.15], [6000, 206.75],
        [7000, 211.45], [8000, 216.1], [9000, 220.75], [10000, 225.4],
        [11000, 230.1], [12000, 234.75], [13000, 239.4], [14000, 243.95],
        [15000, 248.7], [16000, 253.3], [17000, 258], [18000, 262.65],
        [19000, 267.3], [20000, 271.95], [21000, 276.6], [22000, 281.3],
        [23000, 285.9], [24000, 290.5], [25000, 295.25], [26000, 299.85],
        [27000, 304.55], [28000, 309.15], [29000, 313.85], [Infinity, 999999]
    ].find(([t]) => total <= t)[1];


console.log([0, 1, 3300, 3500, 3999, 4000, 4001, 5000, 10000, 100000].map(getValue));

票数 1
EN

Stack Overflow用户

发布于 2021-03-12 21:44:28

请使用开关外壳,这在优化和性能方面是理想的。这样,代码执行直接跳到相应的条件,而不是遍历每个if else if条件。

代码语言:javascript
复制
var total = 6301;
var warranty;

switch (true) {
   case (0 < total && total <= 3300): warranty = 194.25; break;
   case (3300 < total && total <= 4000): warranty = 194.25; break;
   ...
   default: warranty = 999999; break;
}
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66600938

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档