【问题标题】:Is there a short way to check whether the user input ends with one of the elements in an array?是否有一种简短的方法来检查用户输入是否以数组中的一个元素结尾?
【发布时间】:2021-12-31 17:46:22
【问题描述】:

是否有一种简单的方法可以确定用户输入是否 (endsWith()) 数组中的某个元素结尾?我想检查用户输入是否以以下后缀之一结尾:“ss”、“ll”、“tt”。如果用户输入以其中一个后缀结尾,则代码应警告消息“条件满足”,否则应警告“条件失败”。我可以使用简单的 if 语句来做到这一点,如下面的代码所示:

    var inp = document.getElementById("inp");
   

    function findingSuffixes(){

    if(inp.value.endsWith('ss') || inp.value.endsWith('tt') || inp.value.endsWith('ll')){
    alert('condition met')
    } else {
    alert('condition failed')
    }
    
    }
    <p>Enter a word:</p>
    <input type="text" id="inp">
    <button onclick="findingSuffixes()">Find out</button>

但这有点绕。

我试图从不同的角度来看待它。我创建了一个数组并将后缀存储在其中,然后遍历该数组并将 array[i] 作为参数传递给 endsWith 方法,但它没有按预期工作。

    var inp = document.getElementById("inp");
    suffixes = ["ss", "tt", "ll"];



    function findingSuffixes(){


    for(var i=0; i < suffixes.length; i++){

    if(inp.value.endsWith(suffixes[i])){
    alert("condition Met")
    } else {
    alert("condition failed")
    }
    
    } 
   
    
    } 
    <p>Enter a word:</p>
    <input type="text" id="inp">
    <button onclick="findingSuffixes()">Find out</button>

TL'DR:是否有一种简单的方法可以检查用户输入是否以数组中的某个元素结尾?

【问题讨论】:

  • 你总是有两个字符吗?你想知道哪一个匹配吗?
  • @NinaScholz:不,不总是两个字符。对于我的项目,我最后有五个字符,但是为了方便和清晰,我在这个问题中只选择了 2。我想知道用户输入是否以 any 的数组元素结尾。
  • 为什么不使用正则表达式?
  • 使用suffixes.some()
  • @NinaScholz:我不熟悉正则表达式(虽然我很想知道它们是否可以在这里使用)。

标签: javascript arrays loops ends-with


【解决方案1】:

您的代码的问题在于它仍在循环中发出alert。您只希望它在确定时声明“满足”或“失败”。我调整了代码以显示如何做到这一点。一旦我们满足条件,我使用break 退出循环,因为不需要进一步查看。

var inp = document.getElementById("inp");
var suffixes = ["ss", "tt", "ll"];

function findingSuffixes(){
  var msg = "condition failed";
  for(const suffix of suffixes){
    if (inp.value.endsWith(suffix)){
      msg = "condition Met";
      break;  // no need to continue in the loop
    } 
  } 
  alert(msg);
}
<p>Enter a word:</p>
    <input type="text" id="inp">
    <button onclick="findingSuffixes()">Find out</button>

【讨论】:

    【解决方案2】:

    正如@Barmar 所说,您最好使用.some()

    var inp = document.getElementById("inp"),
        sfs = ["ss", "tt", "ll"];
    
    
    
    function findingSuffixes() {
      alert(sfs.some(sf => inp.value.endsWith(sf)) ? "Condition met"
                                                   : "Condition failed");
    }
    <p>Enter a word:</p>
    <input type="text" id="inp">
    <button onclick="findingSuffixes()">Find out</button>

    【讨论】:

      【解决方案3】:

      您可以使用regular experssion 并测试该表达式的字符串。

      /(ll|ss|tt)$/
       (        )   a group
        ll|ss|tt    alternatives with ll ss or tt
                 $  followd by an end of string
      

      function findingSuffixes() {
          if (/(ll|ss|tt)$/.test(inp.value)) {
              console.log('condition met');
          } else {
              console.log('condition failed');
          }
      }
      
      const inp = document.getElementById("inp");
      <p>Enter a word:</p>
      <input type="text" id="inp">
      <button onclick="findingSuffixes()">Find out</button>

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-06-27
        • 1970-01-01
        • 2012-09-06
        • 1970-01-01
        • 1970-01-01
        • 2014-12-19
        • 2013-09-28
        相关资源
        最近更新 更多