【问题标题】:Append onto variable without duplicating value?附加到变量而不重复值?
【发布时间】:2017-12-17 14:06:42
【问题描述】:

我有 12 个包含布尔值的变量。根据每个为真的布尔输入,我将一个数值分配给另一个名为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循环?使用数组?

【问题讨论】:

  • 任何时候你发现自己在写这样的编号变量,你可能应该改用数组。然后你可以写一个循环来处理数组元素。
  • 另外,如果您知道变量是真/假,那么只需执行if (variable) { ... }

标签: javascript for-loop foreach while-loop ternary-operator


【解决方案1】:

又快又脏:

var output = "";
if( boolean1Input  ) output += "527,";
if( boolean2Input  ) output += "528,";
if( boolean3Input  ) output += "529,";
if( boolean4Input  ) output += "530,";
if( boolean5Input  ) output += "531,";
if( boolean6Input  ) output += "532,";
if( boolean7Input  ) output += "533,";
if( boolean8Input  ) output += "534,";
if( boolean9Input  ) output += "535,";
if( boolean10Input ) output += "536,";
if( boolean11Input ) output += "537,";
if( boolean12Input ) output += "538";
return output;

请注意,这可能有一个尾随 , 字符,您必须处理它。

又快又脏,但使用数组作为输出:

var output = [];
if( boolean1Input  ) output.push("527");
if( boolean2Input  ) output.push("528");
if( boolean3Input  ) output.push("529");
if( boolean4Input  ) output.push("530");
if( boolean5Input  ) output.push("531");
if( boolean6Input  ) output.push("532");
if( boolean7Input  ) output.push("533");
if( boolean8Input  ) output.push("534");
if( boolean9Input  ) output.push("535");
if( boolean10Input ) output.push("536");
if( boolean11Input ) output.push("537");
if( boolean12Input ) output.push("538");
return output;

简洁,但危险:

var output = [];
for( var i = 1; i <= 12; i++ ) {
    var input = eval('boolean' + i + 'Input');
    if( input ) output.push( i + 526 ); 
}

【讨论】:

    【解决方案2】:

    使用数组而不是大量单独的变量。

    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);
    

    【讨论】:

      【解决方案3】:

      你可以使用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()

      【讨论】:

        【解决方案4】:

        我强烈建议您使用数组存储输入,这非常方便。假设我们有这样的输入:

        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];
        

        然后简单地使用这个:

        [...CODES.keys()]
            .filter(x => inputs[x])
            .map(x => CODES[x])
        

        它首先过滤掉所有错误输入,然后将剩余的索引映射到相应的代码。

        但是,如果无法使用数组作为输入,我会编写如下代码:

        [
            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
        

        【讨论】:

          猜你喜欢
          • 2012-10-13
          • 1970-01-01
          • 2019-05-08
          • 2021-06-19
          • 2018-08-01
          • 2014-12-09
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多