簡體   English   中英

測試字符串是否包含字符串數組中的單詞

[英]Test whether a string contains a word from an array of strings

它不起作用。 它總是與其他一起。 我需要使用數組,看看字符串是否包含數組中的任何單詞。 請幫忙,這真煩人

 function buttonClick() { var name = document.getElementById('myText').value; var yourstring = name; var substrings = ['fruit', 'apple']; var length = substrings.length; while (length--) { if (yourstring.indexOf(substrings[length]) != -1) { var outcome = 1; } else { var outcome = 2; } } switch (outcome) { case 1: document.getElementById('fruitvalue').innerHTML = name + 'is ...'; break; case 2: document.getElementById('fruitvalue').innerHTML = name + 'is not ...'; break; } } 
 <body> <center> Last Name:<input type="text" id="myText" value=""> <button onClick="buttonClick()" style="font-family:font; color:blue;" >Submit</button> <h2 id="fruitvalue"></h2> </center> </body> </head> 

因為您正在更新數組中每個項目的結果值。 因此,如果數組中的最后一項不是您的輸入值,則switch語句將僅檢查該值。 因此,您可以使用如下代碼

  function buttonClick() { var name = document.getElementById("myText").value; var yourstring = name; var substrings =['fruit', 'apple']; //only change document.getElementById("fruitvalue").innerHTML = substrings.includes(yourstring) ? // use of includes function to check item in the array (name + " is ...") : (name + " is not ..."); } 
 <body> <center> Last Name:<input type="text" id="myText" value=""> <button onClick="buttonClick()" style="font-family:font; color:blue;">Submit</button> <h2 id="fruitvalue"></h2> </center> </body> 

if while塊是塊,則應首先使用break inside,否則,將在下一次迭代中將結果設置為其他值。

 function buttonClick() { var name = document.getElementById("myText").value; var yourstring = name; var substrings =['fruit', 'apple'], length = substrings.length; while(length--) { if (yourstring.indexOf(substrings[length])!==-1) { var outcome=1; break; } else { var outcome=2; } } switch (outcome) { case 1 : document.getElementById("fruitvalue").innerHTML = (name + "is ..."); break; case 2 : document.getElementById("fruitvalue").innerHTML = (name + "is not ..."); break; } } 
 <body> <center> Last Name:<input type="text" id="myText" value=""> <button onClick="buttonClick()" style="font-family:font;color:blue;">Submit</button> <h2 id="fruitvalue"></h2> </center> </body> 

優化版本在這里:

 function buttonClick() { const yourstring = document.getElementById("myText").value; const substrings =['fruit', 'apple'] document.getElementById("fruitvalue").innerHTML = substrings.filter(s => yourstring.indexOf(s) !== -1).length ? (yourstring + " is ...") : (yourstring + " is not ...") } 
 <body> <center> Last Name:<input type="text" id="myText" value=""> <button onClick="buttonClick()" style="font-family:font;color:blue;">Submit</button> <h2 id="fruitvalue"></h2> </center> </body> 

我建議您取消循環和ifs,並通過使用array.someString.prototype.includes大大減少代碼。 這段代碼可以清楚地寫成一行:

  function hasWord(str, words) { return words.some(word => str.includes(word)); } const name = document.getElementById('myText').value; const substrings = ['fruit', 'apple']; const element = document.getElementById('fruitvalue'); function buttonClick() { const matches = hasWord(name, substrings); const suffix = matches ? 'is ...' : 'is not ...'; element.innerHTML = `${name} ${suffix}`; } 
  <body> <center> Last Name: <input type="text" id="myText" value=""> <button onClick="buttonClick()" style="font-family:font; color:blue;" >Submit</button> <h2 id="fruitvalue"></h2> </center> </body> 

嘗試這個。

function buttonClick() {
  var name = document.getElementById('myText').value;
  var yourstring = name;
  var substrings = ['fruit', 'apple'];

    if (substrings.includes(yourstring)) {
      var outcome = 1;
    } else {
      var outcome = 2;
    }

  switch (outcome) {
    case 1:
      document.getElementById('fruitvalue').innerHTML = name + ' is ...';
      break;
    case 2:
      document.getElementById('fruitvalue').innerHTML = name + ' is not ...';
      break;
  }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM