我正在寻找一个函数来比较一个数组中匹配的值.它应该是序列依赖的.这意味着,即第一个数组中的第一个对象应该与第二个数组中第一个对象的相等性进行比较,依此类推.我实际上看了这个,但只有长度比较,长度在我的情况下总是相同.数组中可能的对象是1,2,3,4,5,6,7,8,9
.我应该拆分数组然后比较它们,何时是如何?
这是两个例子:
var array1 = ["3","4","2"]; var array2 = ["9","4","7"]; // result = 1
第二个例子:
var array1 = ["9","4","7","3"]; var array2 = ["3","4","7","2"]; // result = 2
Alexander T... 5
试试这个
var array1 = ["3","4","2"];
var array2 = ["9","4","7"];
function equal(array1, array2) {
var len = array1.length, i, count = 0;
for (i = 0; i < len; i++) {
if (array1[i] === array2[i]) {
count++;
}
}
return count;
}
console.log(equal(array1, array2));
试试这个
var array1 = ["3","4","2"];
var array2 = ["9","4","7"];
function equal(array1, array2) {
var len = array1.length, i, count = 0;
for (i = 0; i < len; i++) {
if (array1[i] === array2[i]) {
count++;
}
}
return count;
}
console.log(equal(array1, array2));