0
我试图在循环中执行语句,只有当所有先前的条件都不符合并且不返回(结束迭代)。但是即使循环显然结束了它的迭代,该语句仍在执行。在Javascript中跳过循环迭代 - 无法让它正常工作
这一切都发生在'catagorize words'部分的'parse'方法中。只有在其他条件不适用的情况下,我才想执行的声明是'添加未分类'声明。我没有包含包含'items'的对象,因为它有很多其他你不需要看到的东西。但如果你这样做可以在这里找到: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
不,我刚刚检查过,不是。你可以看到上面的forEach循环结束,除非Glitch告诉我谎言。这个陈述恰好在for循环结束之前。 – Cuckoo
@Cuckoo检查我的更新! – hyper0009
谢谢,但它仍然没有解决我的问题。我已经使用.map改写它。这是一个更清洁,它只是不正确返回。 – Cuckoo