【问题标题】:how to find successive elements in array如何找到数组中的连续元素
【发布时间】:2021-02-17 13:21:39
【问题描述】:

我正在用 js 实现搜索,核心思想是我有一个单词数组可供搜索,顺其自然

const arr = ['i', 'cant', 'solve', 'this', 'problem', 'alone']

我有一个搜索输入值(可以是一个或两个单词,或者更多),就这样吧

const string = 'i cant swim'

这个输入不会给我任何结果,因为实际上我们在数组中没有连续的这三个单词。但是如果我们采取

const string = 'i cant'

它应该将['i', 'cant'] 作为搜索数组中的两个连续单词返回。我该如何实施? 我的尝试如下:

    const search = []
    const searchStringArr = string.split(' ')       // ['i', 'cant']
    searchStringArr.forEach(str => {
     search.push(arr.filter(word => word.toLowerCase().includes(str.toLowerCase())))
    })

但它返回找到的所有单词的数组,不考虑它们在搜索数组中的连续性。有什么解决办法吗?

【问题讨论】:

    标签: javascript arrays search


    【解决方案1】:

    这是一种幼稚的方法。如果 arr 包含这两个,您还需要匹配 ['can't' 'swim'] 吗? (即不仅从字符串的开头匹配)?

    这里有一个解决方案:

    function findConsecutiveMatches(string, arr){
      const searchStringArr = string.split(' ');    // ['i', 'cant', 'swim']
      let search = [];
    
      arr.forEach((el, idx) => {
        if (searchStringArr[0].toLowerCase() != arr[idx].toLowerCase()){
          return;
        }
        search.push(el);
        if (idx == arr.length -1){
          return;
        }
        for (let diff = 1; diff <= Math.min(arr.length - idx + 1, searchStringArr.length -1); diff++){
          if (searchStringArr[diff].toLowerCase() != arr[idx + diff].toLowerCase()){
            return;
          }
          search.push(arr[idx + diff]);
        }
      });
      
      if (search.length != searchStringArr.length){
        search = [];
      }
      
      console.log(search);  
      return search;
    }
    
    const arr0 = ['i', 'cant', 'solve', 'this', 'problem', 'alone'];
    const string0 = "I cant";
    findConsecutiveMatches(string0, arr0);
    // output: ['i', 'cant'];
    
    const arr1 = ['i', 'cant', 'solve', 'this', 'problem', 'alone'];
    const string1 = "I cant swim";
    findConsecutiveMatches(string1, arr1);
    // output: [];
    
    const arr2 = ['i', 'cant', 'solve', 'this', 'problem', 'alone'];
    const string2 = "solve this";
    findConsecutiveMatches(string2, arr2);
    // output: ['solve', 'this'];
    
    const arr3 = ['i', 'cant', 'solve', 'this', 'problem', 'alone'];
    const string3 = "solve this question";
    findConsecutiveMatches(string3, arr3);
    // output: [];
    
    const arr4 = ['i', 'cant', 'solve', 'this', 'problem', 'alone'];
    const string4 = "I solve this";
    findConsecutiveMatches(string4, arr4);
    // output: [];

    输出示例:

    const arr0 = ['i', 'cant', 'solve', 'this', 'problem', 'alone'];
    const string0 = "I cant";
    findConsecutiveMatches(string0, arr0);
    // output: ['i', 'cant'];
    
    const arr1 = ['i', 'cant', 'solve', 'this', 'problem', 'alone'];
    const string1 = "I cant swim";
    findConsecutiveMatches(string1, arr1);
    // output: [];
    
    const arr2 = ['i', 'cant', 'solve', 'this', 'problem', 'alone'];
    const string2 = "solve this";
    findConsecutiveMatches(string2, arr2);
    // output: ['solve', 'this'];
    
    const arr3 = ['i', 'cant', 'solve', 'this', 'problem', 'alone'];
    const string3 = "solve this question";
    findConsecutiveMatches(string3, arr3);
    // output: [];
    
    const arr4 = ['i', 'cant', 'solve', 'this', 'problem', 'alone'];
    const string4 = "I solve this";
    findConsecutiveMatches(string4, arr4);
    // output: [];
    

    【讨论】:

    • 你看,也许我解释得不好,因为英语不是我的母语,但如果我搜索I solve - 它需要返回空数组,因为它们在搜索中不是一个接一个 - arr。如果我搜索 solve this - 它应该返回 ['solve', 'this'],因为它们满足条件。如果搜索solve, this, question - 返回空数组,因为在搜索数组中我们没有连续出现这个短语,我们有solve, this, problem 代替......希望我解释得更好......
    【解决方案2】:

    你可能想看看useful methods for arrays

    const search = []
        const searchStringArr = string.split(' ')       // ['i', 'cant']
        searchStringArr.forEach(str => {
         if (arr.some(item => item.toLowerCase() === str.toLowerCase())) search.push(str)
        })
    

    【讨论】:

    • 谢谢,但它总是返回一个包含所有找到的单词的数组。只有当它们在搜索数组中连续(一个接一个)时,我才需要返回找到的单词。如果不是 - 返回一个空数组
    【解决方案3】:

    您应该将搜索字符串拆分为一个数组(基于空格分隔符),然后将所有单词与您的数组进行比较。

    const arr = ['i', 'cant', 'solve', 'this', 'problem', 'alone'];
    
    const search = 'i cant swim';
    
    var wordsArray = search.split(" ");
    
    var result = [];
    
    wordsArray.forEach( searchWord => {
      if(arr.includes(searchWord)) {
        result.push(searchWord);
      }
    });
    
    console.log( result );

    【讨论】:

    • 谢谢,但它总是返回一个包含所有找到的单词的数组。只有当它们在搜索数组中连续(一个接一个)时,我才需要返回找到的单词。如果不是 - 返回一个空数组
    猜你喜欢
    • 2011-11-13
    • 2023-04-05
    • 2011-02-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多