满足子字符串条件时,如何返回数组的长度?我有三个数组:

arr1 = ["V1","V1","V1","V1","V1","V2","V2","V2"...]
arr2 = ["A1","A1","B1","B1","B1","A2","A2","A2"...]
arr3 = ["V1A1*","V1A1*","V1B1*","V1B1*"...]


如何返回经过过滤的arr3的长度,其中arr1 [i] + arr2 [i]是元素的子字符串? (“ V1A1”)

对于第一次迭代,此处的预期输出为2。 (i = 0)

提前致谢!

最佳答案

看来您在说这三个数组的长度相同,并且对于数组的每个索引i,您想知道arr1[i] + arr2[i]是否为arr3[i]的子字符串。然后,您想知道有多少元素满足此条件。

为此,您需要查看数组的索引并使用string.indexOf方法查看是否满足您的条件。

var length = arr1.length,
    matchCount = 0,
    isMatch, i;

for(i = 0; i < length; i += 1) {
    // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/indexOf
    // indexOf returns the array index where the substring is found, or -1 if it is not found
    isMatch = arr3[i].indexOf(arr1[i] + arr2[i]) > -1;
    if (isMatch) {
        matchCount += 1;
    }
}

console.log(matchCount);

关于javascript - 寻找子字串时无法传回阵列的长度,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/54713940/

10-09 13:51