【问题标题】:Difficulty returning an array of indexes of each element from an array that matches search term难以从与搜索词匹配的数组中返回每个元素的索引数组
【发布时间】:2011-11-13 16:09:02
【问题描述】:

您好,我真的很难为“从包含字符串的已填充数组返回索引数组”的函数编写代码,当它与搜索词匹配时。 因此,如果搜索词匹配数组元素中的一个或多个单词,无论其顺序如何,它都应该返回这些单词在数组中明显存在的索引。不使用 jquery grep 功能。 这是一些代码来说明我的意思。

array_test = ["Today was hot","Tomorrow will be hot aswell", "Yesterday the weather was not so good","o1234 t12345"]

function locateSearch(array_test,searchTerm){ 
var myArray = [];

for(i=0;i<array_test.length;i++){ 
if(...) // what should i be testing here, this is where i have been going wrong... 

myArray[myArray.length] = i; 
} 
 return myArray;  
}

document.write(locateSearch,'ot') //this should return indexes [0,1,2,3]

对不起,如果我没有很好地解释这一点,感谢您的帮助。谢谢。

【问题讨论】:

    标签: arrays function pattern-matching javascript


    【解决方案1】:
    array_test = ["Today was hot","Tomorrow will be hot aswell", "Yesterday the weather was not so good","o1234 t12345"];
    
    function locateSearch(array_test,searchTerm){
        searchTerm = searchTerm.toLowerCase();
        var myArray = [];
        for(var i = 0; i < array_test.length; i++){
            for(var j = 0; j < searchTerm.length; j++) {
                if(array_test[i].toLowerCase().indexOf(searchTerm[j]) >= 0) {
                    myArray.push(i);
                    break;
                }
            }
        }
        return myArray;  
    }
    
    alert(locateSearch(array_test, 'To').toString());
    

    另见jsfiddle

    === 更新 ===

    如果每个字符都必须在同一个字符串中:

    array_test = ["Today was hot","Tomorrow will be hot aswell", "Yesterday the weather was not so good","o1234 t12345"];
    
    function locateSearch(array_test,searchTerm){
        searchTerm = searchTerm.toLowerCase();
        var myArray = [];
        var bFound;
        for(var i = 0; i < array_test.length; i++){
            bFound = true;
            for(var j = 0; j < searchTerm.length; j++) {
                if(array_test[i].toLowerCase().indexOf(searchTerm[j]) == -1) {
                    bFound = false;
                    break;
                }
            }
            if (bFound) {
                myArray.push(i);
            }
        }
        return myArray;  
    }
    
    alert(locateSearch(array_test, 'es').toString());
    

    另见我更新的jsfiddle

    【讨论】:

    • 谢谢。它可以工作,但我怎样才能做到这一点,如果搜索词有“To”,它会返回索引 [0,2],这是我无法理解的......谢谢
    • 你看到我的最新版本了吗?
    • 嗨,我只是在测试我的一些数据。它运作良好,非常感谢您的时间。我一直在尝试不同的 searchTerms,我尝试了“es”,它应该返回 [1,2] 但它返回 [0,1,2],不知道为什么?再次感谢,对不起,我很痛苦。你肯定在理解我的错误解释方面做得很好! :)
    • 它还返回0,因为第一个字符串中的s。必须是同一字符串中的所有字符吗?我举个例子,一会儿。
    • 我添加了一个替代解决方案,如果每个字符都必须在同一个字符串中。
    【解决方案2】:

    如果我正确理解了您的问题...

    if(array_test[i].indexOf(searchTerm).toLowercase() != -1)
        myArray.push(i);
    

    你没有提到搜索是否区分大小写,但假设是我把它扔在那里

    【讨论】:

    • indexOf 返回一个数字,没有理由将其小写!你的意思是 array_test[i].toLowerCase().indexOf(searchTerm.toLowerCase() ) 你需要将它们都小写并进行比较......或者按照我的建议使用正则表达式。
    【解决方案3】:

    如果您需要测试不区分大小写,请使用正则表达式匹配方法而不是 indexOf()

    所以...if(array[i].match(/searchTerm/i) != null){ myArray.push(i) }

    【讨论】:

      【解决方案4】:

      试试这个:

      array_test = ["Today was hot","Tomorrow will be hot aswell", "Yesterday the weather was not so good","o1234 t12345"]
      
      function locateSearch(array_test,searchTerm) { 
          var myArray = [];
      
          for(i=0;i<array_test.length;i++) { 
              if(array_test[i].indexOf(searchTerm) != -1) // what should i be testing here, this is where i have been going wrong... 
      
              myArray.push(i); 
          } 
          return myArray;  
      }
      
      document.write(locateSearch,'ot') //this should return indexes [0,1,2,3]
      

      这将返回 array_test 中包含搜索词的所有元素的索引。

      【讨论】:

      • 刚刚注意到:代码中的最后一行可能不是您想要做的。您可能应该将其更改为 document.write(locateSearch('ot').join(','))
      • 嘿,这很好用。我怎样才能让它找到'array_test'中的字符,无论它们以什么顺序出现。所以基本上,如果数组中的一个元素中有这些字符,它也应该返回该索引。不过谢谢
      • 如果可行,请接受答案 :) 我不明白您的后续问题,也许可以打开一个更详细的新问题?
      • 所以基本上,如果我输入 'to' 而不是 'ot',我怎样才能让它返回索引 [0,1,2,3]。因为array_test 中的所有元素都有字符'to'。希望这是有道理的:)谢谢。
      • 更改此位: if(array_test[i].indexOf(searchTerm) != -1 || array_test[i].indexOf(searchTerm.reverse()) != -1) 。请接受答案!
      猜你喜欢
      • 2016-10-31
      • 2020-02-08
      • 2018-11-13
      • 1970-01-01
      • 2017-12-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-03
      • 1970-01-01
      相关资源
      最近更新 更多