[英]Array comparison doesnt work
I have a working array comparison (tested with different kind of code), which looks like this: 我有一个有效的数组比较(使用不同类型的代码测试),如下所示:
var a = [0,1,2] var b = [1,2] var match = [] var miss = [] func(a,b, match, miss); function func(a, b, matches, misses) { for (var i=0; i<b.length; i++) { for (var j=0; j<a.length; j++) { if (b[i] == a[j]) { console.log("Found"); matches.push(b[i]) } } } console.log("Matches: " + matches); console.log("Misses: " + misses); }
It works usually fine. 通常正常。 However this time it doesn't.
但是这次没有。 I have two arrays which have a different size (a is bigger than b) and can have different content like "100.AB 12345".
我有两个数组,它们的大小不同(a大于b),并且可以具有不同的内容,例如“ 100.AB 12345”。
I can't explain why my function doesn't work in that case. 我无法解释为什么我的功能在这种情况下不起作用。 Can someone help me out?
有人可以帮我吗? It doesn't find any matches, everything is a miss, even though they are matches.
它找不到任何匹配项,即使它们是匹配项,也都错过了。
I think it's this line: 我认为这是这行:
if (b[j] == a[j])
Should the b[j]
be b[i]
? b[j]
应该是b[i]
吗? As a
is bigger than b
, using the j
indexer would be attempting to reference after the end of b
. 由于
a
大于b
,因此使用j
索引器将尝试在b
结束之后进行引用。
Hello check the below code it will work fine even a is bigger than b or b is bigger than a 您好,请检查以下代码,即使a大于b或b大于a也会正常工作
var a = ['a','ab','c','d','e']; var b = ['ef','f','ab','a']; var a1,b1; var match = []; var miss = []; var temp_a=a.length; var temp_b=b.length; if( temp_a > temp_b){ a1=b; b1=a; }else{ a1=a; b1=b; } func(a1,b1, match, miss); function func(a, b, matches, misses) { for (var i=0; i<b.length; i++) { for (var j=0; j<a.length; j++) { if (b[i] == a[j]) { console.log("Found"); matches.push(b[i]) } } } console.log("Matches: " + matches); console.log("Misses: " + misses); }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.