gpt4 book ai didi

javascript - 如何一次切换多个值/列表中的值?

转载 作者:行者123 更新时间:2023-12-02 17:28:52 26 4
gpt4 key购买 nike

有没有一种有效的方法可以同时切换多个变量的 bool 值?我的变量是我想要根据 userValue 显示/隐藏的行。

myList = [lineA, lineB, lineC, lineD]

我下面的示例是非常硬编码的。有很多重复的代码。有更聪明的方法吗?

if (userValue == 'A') {
lineA.visible = true;
lineB.visible = false;
lineC.visible = false;
lineD.visible = false;
} else if (userValue == 'B') {
lineA.visible = false;
lineB.visible = true;
lineC.visible = false;
lineD.visible = false;
} else if (userValue == 'C') {
lineA.visible = false;
lineB.visible = false;
lineC.visible = true;
lineD.visible = false;
} else if (userValue == 'D') {
lineA.visible = false;
lineB.visible = false;
lineC.visible = false;
lineD.visible = true;
}

最佳答案

开关盒会更好:

//initialize to false
lineA.visible = false;
lineB.visible = false;
lineC.visible = false;
lineD.visible = false;

switch(userValue ) {
case 'A':
lineA.visible = true;
break;
case 'B':
lineB.visible = true;
break;
case 'C':
lineC.visible = true;
break;
case 'D':
lineD.visible = true;
break;
default:
// do something
}

关于javascript - 如何一次切换多个值/列表中的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56457441/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com