我有一个字符数组,我正在测试另一个数组。每次运行应用程序时,字符串长度数组都可以动态更改。这是我的代码,但现在它只是通过数组的第一个索引。如何让它遍历数组toTest
代码:
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/
答案 0 :(得分:1)
您需要做的就是添加一个for循环来遍历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');
}
}
}