简体   繁体   English

针对另一个数组测试字符串数组indexOf?

[英]Testing an array of strings indexOf against another array?

I have an array of characters I am testing another array against. 我要测试另一个数组的字符数组。 The array of strings length can dynamically change every time the app is ran. 每次运行应用程序时,字符串长度数组都可以动态更改。 Here is the code I have, but right now it is just going through the first index of the array. 这是我的代码,但是现在它只是通过数组的第一个索引。 How can I have it go through all the length of the array toTest 我怎样才能遍历整个数组toTest

code: 码:

var testAgainst = ['and','+',',','&'];
var toTest = ['This is just a test +','This is just a test and','This is just a test ,','This is just a test &','This is just a test'];

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

    if (toTest[0].indexOf(testAgainst[i]) > -1){
        console.log('match: ' + testAgainst[i]);
    }else{
        console.log('no match');
    }

}

JSFIDDLE: https://jsfiddle.net/bmpgsxb2/2/ JSFIDDLE: https ://jsfiddle.net/bmpgsxb2/2/

All you need to do is add a for-loop to go through each element in the toTest array like so: 您需要做的就是添加一个for循环来遍历toTest数组中的每个元素,如下toTest

for(var i = 0; i < testAgainst.length; i++){
    for(var j = 0; j < toTest.length; j++){
        if (toTest[j].indexOf(testAgainst[i]) > -1){
            console.log('match: ' + testAgainst[i]);
        }else{
            console.log('no match');
        }
    }
}

Fiddle 小提琴

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM