【问题标题】:How to know if a string includes at least one element of array in Javascript如何知道一个字符串是否在Javascript中至少包含一个数组元素
【发布时间】:2020-02-05 03:11:11
【问题描述】:
let theString = 'include all the information someone would need to answer your question'

let theString2 = 'include some the information someone would need to answer your question'

let theString3 = 'the information someone would need to answer your question'

let theArray = ['all', 'some', 'x', 'y', 'etc' ]

theString 为真,因为存在“全部”, theString2 为真,因为有“一些”, theString3 false 因为没有“全部”或“某些”

【问题讨论】:

  • 但是在theString3 中有 some -- 为什么它会返回false
  • 您能否提供更多关于您如何定义“包含”的详细信息?正如上面的评论所提到的,在“某人”中有技术上的“一些”

标签: javascript arrays logic


【解决方案1】:
const theStringIsGood = theString.split(' ').some(word => theArray.includes(word))

此代码将一个句子拆分为每个单词,并检查是否有任何单词与匹配集 theArray 匹配。

【讨论】:

    【解决方案2】:

    您可以使用 string.includes() 方法。但在这种情况下,theString3 仍然会返回 true,因为 some 在某些东西中。您可以创建一个辅助函数来查看字符串是否在数组中:

    function findString(arr, str) {
        let flag = false;
        let strArr = str.split(' ');
        arr.forEach(function(s) {
            strArr.forEach(function(s2) {
                if(s === s2) {
                    flag = true;
                }
            });
        });
        return flag;
    };
    

    现在您可以将数组和其中一个字符串传递给函数并对其进行测试。

    【讨论】:

      【解决方案3】:

      你可以用正则表达式简单地做到这一点:

      function ArrayInText(str, words) {
          let regex = new RegExp("\\b(" + words.join('|') + ")\\b", "g");
          return regex.test(str);
      }
      

      在正则表达式中 \b 是单词边界

      请注意,如果您自己拆分所有字符串并逐个检查,则会占用大量内存,因为这是一个贪婪的解决方案。我使用 javascript 原生函数提供。

      sn-p:

      let theString = 'include all the information someone would need to answer your question'
      
      let theString2 = 'include some the information someone would need to answer your question'
      
      let theString3 = 'the information someone would need to answer your question'
      
      let theArray = ['all', 'some', 'x', 'y', 'etc']
      
      function ArrayInText(str, words) {
          let regex = new RegExp("\\b(" + words.join('|') + ")\\b", "g");
          return regex.test(str);
      }
      
      console.log(ArrayInText(theString, theArray));
      console.log(ArrayInText(theString2, theArray));
      console.log(ArrayInText(theString3, theArray));

      祝你好运:)

      【讨论】:

        【解决方案4】:

        1) isIncludeWord -- 使用 splitincludes 方法查找完全匹配的单词。
        2) isInclude -- 使用someincludes 查找匹配包含。

        let theString =
          "include all the information someone would need to answer your question";
        let theString2 =
          "include some the information someone would need to answer your question";
        let theString3 = "the information someone would need to answer your question";
        let theArray = ["all", "some", "x", "y", "etc"];
        
        const isIncludeWord = (str, arr) =>
          str.split(" ").reduce((include, word) => include || arr.includes(word), false);
        
        const isInclude = (str, arr) => arr.some(item => str.includes(item));
        
        console.log(isIncludeWord(theString, theArray));
        console.log(isIncludeWord(theString2, theArray));
        console.log(isIncludeWord(theString3, theArray));
        
        console.log(isInclude(theString, theArray));
        console.log(isInclude(theString2, theArray));
        console.log(isInclude(theString3, theArray));

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-12-26
          • 2018-08-11
          相关资源
          最近更新 更多