基本上,我需要从包含在主字符串中的数组中获取单词
我在这里有一个循环代码,但我认为有一个单行代码可以做到这一点。我需要对代码进行优化,不仅要优化代码长度,还要优化性能。
谢谢
var aValidWords = ["ex", "exes", "expert",
"experts", "expertise", "sex", "sexes",
"exchange", "change", "changes"];
var sMainWord = "expertsExchange";
var aPossibleWords = new Array();
var sMainWordLower = sMainWord.toLowerCase();
for(i=0; i < aValidWords.length; i++){
var sCurrentWord = aValidWords[i].toLowerCase();
if(sMainWordLower.indexOf(sCurrentWord) != -1){
aPossibleWords.push(aValidWords[i]);
}
}
document.write(aPossibleWords.join("<br />"));