[英]Testing an array of strings indexOf against another array?
我要测试另一个数组的字符数组。 每次运行应用程序时,字符串长度数组都可以动态更改。 这是我的代码,但是现在它只是通过数组的第一个索引。 我怎样才能遍历整个数组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/
您需要做的就是添加一个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');
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.