Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 很难从匹配搜索项的数组中返回每个元素的索引数组_Arrays_Function_Pattern Matching_Javascript - Fatal编程技术网

Arrays 很难从匹配搜索项的数组中返回每个元素的索引数组

Arrays 很难从匹配搜索项的数组中返回每个元素的索引数组,arrays,function,pattern-matching,javascript,Arrays,Function,Pattern Matching,Javascript,您好,我真的很难为一个函数编写代码,该函数“在匹配搜索词时,从已填充的包含字符串的数组中返回索引数组”。 因此,如果搜索项与数组元素中的一个或多个单词匹配,而不考虑其顺序,则它应该返回这些单词在数组中的明显索引。不使用jquerygrep特性。 这里有一些代码来说明我的意思 array_test = ["Today was hot","Tomorrow will be hot aswell", "Yesterday the weather was not so good","o1234 t123

您好,我真的很难为一个函数编写代码,该函数“在匹配搜索词时,从已填充的包含字符串的数组中返回索引数组”。 因此,如果搜索项与数组元素中的一个或多个单词匹配,而不考虑其顺序,则它应该返回这些单词在数组中的明显索引。不使用jquerygrep特性。 这里有一些代码来说明我的意思

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]
array_test=[“今天很热”,“明天也会很热”,“昨天天气不太好”,“o1234 t12345”]
函数locateSearch(数组_测试,searchTerm){
var myArray=[];
对于(i=0;i试试这个:

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=[“今天很热”,“明天也会很热”,“昨天天气不太好”,“o1234 t12345”]
函数locateSearch(数组_测试,searchTerm){
var myArray=[];
对于(i=0;i
array_test=[“今天很热”,“明天也会很热”,“昨天天气不太好”,“o1234 t12345”];
函数locateSearch(数组_测试,searchTerm){
searchTerm=searchTerm.toLowerCase();
var myArray=[];
对于(var i=0;i=0{
myArray.push(i);
打破
}
}
}
返回myArray;
}
警报(locateSearch(数组_测试,'To').toString());
也看到这个

==更新===

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

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());
array_test=[“今天很热”,“明天也会很热”,“昨天天气不太好”,“o1234 t12345”];
函数locateSearch(数组_测试,searchTerm){
searchTerm=searchTerm.toLowerCase();
var myArray=[];
var bFound;
对于(var i=0;i

另请参阅我的更新。

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

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

您没有提到搜索是否区分大小写,但假设是,我将其放在了那里

如果您需要测试不区分大小写,请使用regEx match方法,而不是indexOf()方法


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

刚刚注意到:代码中的最后一行可能不是您想要做的。您可能应该将其更改为document.write(locateSearch('ot').join(','))嘿,这很好用。我怎样才能使它在“array\u test”中找到字符,而不管它们以什么顺序出现。因此,基本上,如果数组中的一个元素中有这些字符,它也应该返回该索引。但是,谢谢。如果行得通,请接受答案:)我不明白你的后续问题,也许可以打开一个更详细的新问题?因此,基本上,如果我输入'to'而不是'ot',我如何让它返回索引[0,1,2,3]。因为数组_测试中的所有元素都有字符'to'。希望有意义:)谢谢。更改为这一位:if(数组_测试[I]。indexOf(搜索项)!=-1 | | array_test[i]。indexOf(searchTerm.reverse())!=-1)。请接受答案!indexOf返回一个数字,没有理由将其小写!您的意思是array_test[i]。toLowerCase()。indexOf(searchTerm.toLowerCase())你需要把它们的大小写都降下来并进行比较……或者像我建议的那样使用正则表达式。谢谢。它是有效的,但是我怎样才能使它在搜索词有“to”时返回索引[0,2]这是我无法理解的…谢谢你看到我的最新版本了吗?嗨,我只是用我的一些数据进行测试。它工作得很好,非常感谢你的时间。我一直在尝试不同的搜索词,我尝试了“es”,它应该返回[1,2],但它返回[0,1,2],不知道为什么?再次感谢,很抱歉让你感到痛苦。你确实很好地理解了我糟糕的解释!:)它还返回
0
,因为第一个字符串中的
s
。必须是同一个字符串中的所有字符吗?我举个例子,等一下。如果每个字符都必须在t中,我已经添加了一个替代解决方案他用同样的绳子。
if(array_test[i].indexOf(searchTerm).toLowercase() != -1)
    myArray.push(i);