【问题标题】:Skipping loop iteration in Javascript - can't get it to work correctly在 Javascript 中跳过循环迭代 - 无法使其正常工作
【发布时间】:2017-07-24 09:21:09
【问题描述】:

我正在尝试在循环中执行一条语句,前提是不满足所有先前的条件并且不返回(结束迭代)。但是,即使循环显然已经结束了它的迭代,该语句仍在执行。

这一切都发生在“分类单词”部分的“解析”方法中。如果其他条件不适用,我只想执行的语句是“添加未分类”语句。我没有包括包含“项目”的对象,因为它有很多其他你不需要看到的东西。但如果你这样做,可以在这里找到:Link

var textParser4 = {
  // It need to store command phrases
  commandWords: ["north", "pick up", "east", "south", "west", "up", "down", "examine", "open", "inventory", "use", "get", "take", "drop", "put dn"],
    // It needs to store filler words - to remove
  fillerWords: ["go", "to", "then", "with", "it", "at", "as", "and", "i"],
  // It needs to store string after it's turned into array
  stringArray: [],
  // It needs somewhere to store seperated strings
  seperateStringArrays: [],
  // It needs a parsing function
  parse: function(inputString) {
     // It needs to make input string lower case.
     inputString = inputString.toLowerCase();
     // It needs to remove punctuation
    inputString = inputString.replace(/[.,\/#!$%\^&\*;:{}=\-_`~()]/g,"");
    // It needs to remove filler words.
    this.fillerWords.forEach(function(word) {
      inputString = inputString.replace(word + " ", "");
    });
    // Turn string into array
    this.stringArray = inputString.split(" ");
    // Remove empty elements
    this.stringArray.forEach(function(element, index) {
      if (element === "") {
        this.stringArray.splice(index, 1);
      }
    }, this);
    // Catagorise words
    for (var i = 0; i < this.stringArray.length; i++) {
      console.log("string array length: ", this.stringArray.length);
      var jWord = this.stringArray[i] + " " + this.stringArray[i + 1];
      this.commandWords.forEach(function(cWord) {
        //find two word commands first
        if (cWord === jWord) {
          this.seperateStringArrays.push({ phrase: this.stringArray[i]  + " " +  this.stringArray[i + 1], wordCatagory: "command" });
          // Remove word that has been joined with previous word
          this.stringArray.splice(i + 1, 1);
          return;
          // find single word commands
        } else if (cWord === this.stringArray[i]) {
          this.seperateStringArrays.push({ phrase: this.stringArray[i], wordCatagory: "command" });
        }
        return;
        // find items words
      }, this);
      // find items
      Game.items.forEach(function(item) {
        if (jWord === item.name) {
          // find two word items
          this.seperateStringArrays.push( { phrase: this.stringArray[i]  + " " +  this.stringArray[i + 1], wordCatagory: "item" });
          // Remove word that has been joined with previous word
          this.stringArray.splice(i + 1, 1);
          return;
          // find single word items
        } else if (this.stringArray[i] === item.name) {
          this.seperateStringArrays.push({ phrase: this.stringArray[i], wordCatagory: "item" });
        }
        return;
      }, this);
      // Add uncatagorised 
      this.seperateStringArrays.push({ phrase: this.stringArray[i], wordCatagory: "uncatagorized" });
    };
    // DOn't forget to empty string array after new arrays are created
    console.log(this.seperateStringArrays);
    // sort sentences into seperate commands
    this.seperateStringArrays = [];
  }
}

// catagorise word
// loop through string array
// check if word or pair of words match a command
  // If so, end iteration and start next
// Check if word or word pair match an item
  // If so, end iteration and start next iteration

【问题讨论】:

    标签: javascript for-loop iteration


    【解决方案1】:

    如果我没有遗漏什么,这行代码:

    this.seperateStringArrays.push({ phrase: this.stringArray[i], wordCatagory: "uncatagorized" });
    

    在 forEach 迭代之外运行,因此很明显,一旦迭代完成,它将继续执行,以解决这个问题;尝试将其作为else if 放入循环中并将其放在循环的底部,因为任何其他计算结果为true 的语句都将退出循环并在到达之前中断。

      Game.items.forEach(function(item) {
        if (jWord === item.name) {
          // find two word items
          this.seperateStringArrays.push( { phrase: this.stringArray[i]  + " " +  this.stringArray[i + 1], wordCatagory: "item" });
          // Remove word that has been joined with previous word
          this.stringArray.splice(i + 1, 1);
          return;
          // find single word items
        } else if (this.stringArray[i] === item.name) {
          this.seperateStringArrays.push({ phrase: this.stringArray[i], wordCatagory: "item" });
        }
        //End of if statement
        return;
      }, this);
      //End of forEach
    this.seperateStringArrays.push({ phrase: this.stringArray[i], wordCatagory: "uncatagorized" });
    

    【讨论】:

    • 不,我刚刚检查过,但不是。你可以看到上面 forEach 循环的结尾,除非 Glitch 是在告诉我谎言。有问题的语句正好在 for 循环结束之前。
    • 谢谢,但它仍然没有解决我的问题。我改用 .map 重写了它。它干净多了,只是还没有正常返回。
    猜你喜欢
    • 2021-03-29
    • 1970-01-01
    • 2023-03-04
    • 1970-01-01
    • 2021-09-26
    • 2015-06-08
    • 2020-09-21
    • 1970-01-01
    相关资源
    最近更新 更多