Javascript 附加到变量而不复制值?
我有12个包含布尔值的变量。基于每一个为真的布尔输入,我为另一个名为Javascript 附加到变量而不复制值?,javascript,for-loop,foreach,while-loop,ternary-operator,Javascript,For Loop,Foreach,While Loop,Ternary Operator,我有12个包含布尔值的变量。基于每一个为真的布尔输入,我为另一个名为finalInputValue的变量指定一个数值。我遇到的问题是,如果不复制一个值,我就无法找出如何附加到此变量finalInputValue。到目前为止,我有以下代码: var finalInputValue = "" function findFinalInputValue(){ if (finalInputValue == ""){ finalInputValue
finalInputValue
的变量指定一个数值。我遇到的问题是,如果不复制一个值,我就无法找出如何附加到此变量finalInputValue
。到目前为止,我有以下代码:
var finalInputValue = ""
function findFinalInputValue(){
if (finalInputValue == ""){
finalInputValue = boolean1Input == true ? "527" :
boolean2Input == true ? "528" :
boolean3Input == true ? "529" :
boolean4Input == true ? "530" :
boolean5Input == true ? "531" :
boolean6Input == true ? "532" :
boolean7Input == true ? "533" :
boolean8Input == true ? "534" :
boolean9Input == true ? "535" :
boolean10Input == true ? "536" :
boolean11Input == true ? "537" :
boolean12Input == true ? "538" : "";
}
if (finalInputValue != ""){
...
...
}
}
findFinalInputValue();
如您所见,此块基本上是将第一个数值赋给变量finalInputValue
。如何让函数反复运行,直到它检查完所有12个布尔输入?如何为每个为真的布尔值附加相应的数值,而不让函数复制其中一个值?我还没有开始函数中附加到第一个结果上的部分,因为我不知道如何告诉函数忽略某个布尔输入(如果它已经被附加)。希望我已经说清楚了。我只是想得到所有“真”布尔输入的附加数值字符串。实现这一点最简单的方法是什么?一个for循环?阵列的使用?快速且肮脏:
注意,这可能有一个尾随的,
字符,您必须处理它
快速且脏,但使用数组进行输出:
简洁但危险:
var输出=[];
对于(var i=1;i使用数组,而不是大量单独的变量
function findFinalInputValue(inputs) {
if (finalInputValue == "") {
for (var i = 0; i < inputs.length; i++) {
if (inputs[i] == true) {
finalInputValue += (527 + i).toString();
}
}
}
if (finalInputValue != "") {
...
}
}
findFinalInputValue(booleanInputs);
函数findFinalInputValue(输入){
如果(finalInputValue==“”){
对于(变量i=0;i
U可以使用json跟踪附加值
Var finalvalue = {}
Var booleaninput = boolean1Input == true ? "527" :""
If(booleaninput != "") {
If(typeof finalvalue.booleaninput! = "undefined") {
//value already present
}else{
//add value to json
Finalvalue[booleanvalue] = booleanvalue
}
}
在循环完成后,您可以通过获取所有键来获得最终值
Finalvalue.Keys()我强烈建议您使用数组存储输入,这非常方便。假设我们有这样的输入:
const CODES = ["527", "528", "529", "530", "531", "532", "533", "534", "535", "536", "537", "538"];
const inputs = [boolean1Input, boolean2Input, boolean3Input, boolean4Input, boolean5Input, boolean6Input, boolean7Input, boolean8Input, boolean9Input, boolean10Input, boolean11Input, boolean12Input];
[
boolean1Input && "527",
boolean2Input && "528",
boolean3Input && "529",
boolean4Input && "530",
boolean5Input && "531",
boolean6Input && "532",
boolean7Input && "533",
boolean8Input && "534",
boolean9Input && "535",
boolean10Input && "536",
boolean11Input && "537",
boolean12Input && "538",
].filter(x => x) // filter out false values
然后简单地使用这个:
[...CODES.keys()]
.filter(x => inputs[x])
.map(x => CODES[x])
它首先过滤掉所有错误的输入,然后将剩余的索引映射到相应的代码
但是,如果无法使用数组进行输入,我将编写如下代码:
const CODES = ["527", "528", "529", "530", "531", "532", "533", "534", "535", "536", "537", "538"];
const inputs = [boolean1Input, boolean2Input, boolean3Input, boolean4Input, boolean5Input, boolean6Input, boolean7Input, boolean8Input, boolean9Input, boolean10Input, boolean11Input, boolean12Input];
[
boolean1Input && "527",
boolean2Input && "528",
boolean3Input && "529",
boolean4Input && "530",
boolean5Input && "531",
boolean6Input && "532",
boolean7Input && "533",
boolean8Input && "534",
boolean9Input && "535",
boolean10Input && "536",
boolean11Input && "537",
boolean12Input && "538",
].filter(x => x) // filter out false values
每当你发现自己在写这样的带编号的变量时,你可能应该改用数组。然后你可以写一个循环来处理数组元素。另外,如果你知道变量是真/假,那么只需执行if(variable){…}
[
boolean1Input && "527",
boolean2Input && "528",
boolean3Input && "529",
boolean4Input && "530",
boolean5Input && "531",
boolean6Input && "532",
boolean7Input && "533",
boolean8Input && "534",
boolean9Input && "535",
boolean10Input && "536",
boolean11Input && "537",
boolean12Input && "538",
].filter(x => x) // filter out false values