简体   繁体   English

循环内的 IF 语句未执行

[英]IF statement inside a loop is not executing

For the life of my I cannot figure out why this if statement never executes.在我的一生中,我无法弄清楚为什么这个 if 语句永远不会执行。 The code below is after I have added in a bunch of logger stuff for debugging, but normally it would just read if(answerArray[z] == answerArray[z-1]) .下面的代码是在我添加了一堆用于调试的记录器之后,但通常它只会读取if(answerArray[z] == answerArray[z-1]) It seems like it should fire on the 3rd pass, but doesn't.似乎它应该在第三遍时触发,但没有。 Does anyone have a clue what is going on?有谁知道发生了什么? I have also tried it with === , but still no dice.我也尝试过=== ,但仍然没有骰子。

/////////////////////////////////////////////////////////////////////////
// Get answer choices
/////////////////////////////////////////////////////////////////////////
  var lastRow = sheet.getLastRow();
  var rawArray= sheet.getRange('D6:D'+lastRow).getValues();  
  var answerArray = rawArray.filter(function(n){return n != ''});   //remove blanks
  answerArray.sort();

/////////////////////////////////////////////////////////////////////////
// Remove Duplicate Answers
/////////////////////////////////////////////////////////////////////////
  var arrayContents = "";
  for (var z = 0;z<answerArray.length;z=z+1) {arrayContents = arrayContents + answerArray[z] + ", ";}

  Logger.log("RemoveDupes Started");
  Logger.log("Array Contents: " + arrayContents);
  Logger.log("answerArray[0] = "+answerArray[0]);
  for(var z = 1; z< answerArray.length; z=z+1){
     // Logger.log("answerArray["+z+"] = "+answerArray[z]);
      var current = answerArray[z];
      var previous = answerArray[z-1];
      Logger.log("Current = " + current+"; Previous = " + previous);
      if(current == previous) 
      {
        Logger.log("Duplicate Found");
          answerArray.splice(z,1);
          //answerArray.splice(z,1); //delete duplicate
          z=z-1;                  //reduce z to account for shortened array
      }
  }
  Logger.log("RemoveDupes Ended");

And here is the log:这是日志:

11:40:53 AM Notice  Execution started
11:41:01 AM Info    RemoveDupes Started
11:41:01 AM Info    Array Contents: A, B, C, C, D, 
11:41:01 AM Info    answerArray[0] = A
11:41:01 AM Info    Current = B; Previous = A
11:41:01 AM Info    Current = C; Previous = B
11:41:01 AM Info    Current = C; Previous = C
11:41:01 AM Info    Current = D; Previous = C
11:41:01 AM Info    RemoveDupes Ended

Solution Thanks everyone for chiming in. I was able to solve it by casting the array to strings.解决方案感谢大家的参与。我能够通过将数组转换为字符串来解决它。 I changed the top section to the following and it works.我将顶部更改为以下内容,并且可以正常工作。

/////////////////////////////////////////////////////////////////////////
// Get answer choices
/////////////////////////////////////////////////////////////////////////
  var lastRow = sheet.getLastRow();
  var rawArray = [""];
  var directInputArray = sheet.getRange('D6:D'+lastRow).getValues();  
  for(var z = 0; z< directInputArray.length; z=z+1){
     rawArray[z] = String(directInputArray[z]);  //cast all values as strings to remove meta-data
  }

  var answerArray = rawArray.filter(function(n){return n != ''});   //remove blanks
  answerArray.sort();

/////////////////////////////////////////////////////////////////////////
// Remove Duplicate Answers
/////////////////////////////////////////////////////////////////////////
  for(var z = 1; z< answerArray.length; z=z+1){
      if(answerArray[z] == answerArray[z-1]) {
          answerArray.splice(z,1);
          z=z-1;                  //reduce z to account for shortened array
      }
  }

Try this code...试试这个代码...

/////////////////////////////////////////////////////////////////////////
// Get answer choices
/////////////////////////////////////////////////////////////////////////
  var lastRow = sheet.getLastRow();
  var answerArray = sheet.getRange('D6:D'+lastRow).getValues().filter(String).sort();   //remove blanks

/////////////////////////////////////////////////////////////////////////
// Remove Duplicate Answers
/////////////////////////////////////////////////////////////////////////
  let unique = answerArray.filter((item, i, ar) => ar.indexOf(item) === i);
  Logger.log(unique);

From here... How to get unique values in an array从这里... 如何获取数组中的唯一值

The solution was posted on the question, so I'll add this here as community wiki so more people can benefit from it:该解决方案已发布在该问题上,因此我将在此处将其添加为社区 wiki,以便更多人可以从中受益:

Solution Thanks everyone for chiming in. I was able to solve it by casting the array to strings.解决方案感谢大家的参与。我能够通过将数组转换为字符串来解决它。 I changed the top section to the following and it works.我将顶部更改为以下内容,并且可以正常工作。

/////////////////////////////////////////////////////////////////////////
// Get answer choices
/////////////////////////////////////////////////////////////////////////
  var lastRow = sheet.getLastRow();
  var rawArray = [""];
  var directInputArray = sheet.getRange('D6:D'+lastRow).getValues();  
  for(var z = 0; z< directInputArray.length; z=z+1){
     rawArray[z] = String(directInputArray[z]);  //cast all values as strings to remove meta-data
  }

  var answerArray = rawArray.filter(function(n){return n != ''});   //remove blanks
  answerArray.sort();

/////////////////////////////////////////////////////////////////////////
// Remove Duplicate Answers
/////////////////////////////////////////////////////////////////////////
  for(var z = 1; z< answerArray.length; z=z+1){
      if(answerArray[z] == answerArray[z-1]) {
          answerArray.splice(z,1);
          z=z-1;                  //reduce z to account for shortened array
      }
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM