JavaScript - 比较两个数组 [英] JavaScript - Compare two arrays

查看:73
本文介绍了JavaScript - 比较两个数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我见过很多关于如何在JavaScript中比较两个数组的帖子,如果你找到匹配的话就做点什么。
我想知道如何写相反的东西。
我有两个数组,当我找不到匹配项时我想做点什么。

I have seen many posts about how to compare two arrays in JavaScript and do something if you find a match. I wanted to know how to write the opposite. I have two arrays and I want to do something when I don't find a match.

var a = [1,2,3,5];
var b = [4,7,5,5];


for(i=0;i<a.length;i++){
    for(j=0;j<b.length;j++){
       if(b[j]===a[i]){
          //do something
       }
   }
 }

 //somehow return 4, 7

基本上,我想找到与上述情况相反的情况。如果我找不到比赛,我想表演一个动作。我的其他声明会在哪里?

Basically, I would like to find the reverse of the above. If I dont find a match I would like to perform an action. Where would my else statement go?

   loop1:                                                                                          
        for(var i=0;i<a.length;i++){                                                                                                                        
            loop2:                                                                                        
            for(var j=0;j<b.length;j++){                                                                         
                if(b[j]==a[i]){                                          
                    console.log("break loop");                                                            
                    break loop2;                                                                          
                }                                                                                         
                else{                                                                                     
                    continue loop1;                                                                              
                }                                                                                         
            }                                                                                             
        }

这就是我我已经走了,我是在正确的轨道上?

This is as far as I've gotten, am I on the right track?

推荐答案

你想检查数组的任何元素的值是否 a [] 存在于数组 b [] 中,而您想要在不匹配的情况下做点什么。

You want to check if the value of any element of array a[] exists in array b[], and you want to "do something" on the condition that there is "no match".

有许多不同的方法可以考虑不匹配的情况。

There are many different ways you can consider a "no match" condition.

在你的第一个例子中,每当找到匹配时做某事,它将检查 a [] <的每个元素的值/ code> ,并将该值逐一与 b [] 的所有元素进行比较。因此,此比较过程不依赖于匹配的位置。此外,如果 a [] 中特定元素的值存在于 b []内的多个位置 ,或者 b [] 中的特定元素的值存在于 a [] ,然后代码将为同一元素的多个匹配中的每个匹配执行某些操作。

In your first example that "does something" each time it finds a match, it will examine the value of each element of a[], and compare that value to all elements of b[], one by one. So, this compare proceedure is not dependent on the position of a match. Also, if the value of a particular element in a[] exists in multiple places within b[], or if the value of a particular element in b[] exists in multiple places within a[], then the code will "do something" for each of the multiple matches of the same element.

因此,对于您提供的样本数据:

So, in the case of the sample data you provided:

var a = [1,2,3,5];
var b = [4,7,5,5];

当代码找到 <$的第4个元素时,它会做点什么 c $ c> a [] 匹配 b [] 的3rd元素,以及它会再次做某事,当它找到 的第4个元素时,[] 匹配的第4个元素 b [] 即可。我认为这就是你想要它的方式,但是如果你发现 a [] 中的元素,你只想一次做某事在 b [] 中的一个或多个位置,您只需要添加中断;声明,紧跟在做某事声明之后。

the code will "do something" when it finds the "4th" element of a[] matches the "3rd" element of b[], and it will "do something" again, when it finds the "4th" element of a[] matches the "4th" element of b[]. I assume that's how you wanted it, but if you only wanted to "do something" one-time if an element in a[] is found in one "or more" places within b[], then you would only need to add a "break;" statement, immediately following the "do something" statement(s).

所以,对于你的问题,你说的是,你想要反对:做 当你找不到匹配的东西。

So, for your question, you said that instead, you want the "opposite": to "do something" when you don't find a match.

从字面上看,你只需更改语句: if(b [j] === a [i]) ,至: if(b [j]!= a [i]) 即可。现在,这可能你想要什么,因为不匹配的情况可能会经常发生:

Taking that literally, you would just change the statement: "if(b[j]===a[i])", to: "if(b[j] != a[i])". Now, this is probably not what you want, because the "no match" condition would likely happen far too often:

for(i=0;i<a.length;i++){
    for(j=0;j<b.length;j++){
        if(b[j] != a[i]){
            // do something
        }
    }
}



另一种可能性是,如果 a []的元素值,你想要做某事 b [] 的任何地方找到code> 。为此,您将检查 a [] 的每个元素的值,并将该值与 <$ c的所有元素进行比较$ c> b [] ,一个接一个,如果在检查 b [] 您找不到与 a [] 当前元素值的匹配项那么 你会做点什么。


Another possibility, is that you want to "do something" if the value of an element of a[] can't be found anywhere in b[]. For this, you would examine the value of each element of a[], and compare that value to all elements of b[], one by one, and if after checking the value of all elements in b[] you couldn't find a match to the value of the current element of a[], then you would "do something".

在这种情况下,如果你找到了来自 <$的当前值的匹配c $ c> a [] ,在 b [] 中,进一步检查该元素 a [] 不是必需的。您可能希望跳过剩下的部分,然后开始检查 code> 的下一个元素 b [] 即可。为此,您需要使用 continue outerloop;

In this case, if you found a match of the current value from a[], in b[], further checking of that element of a[] would not be necessary. You would want skip the rest, and begin checking the next element of a[] to the elements of b[]. For this, you would want to use "continue outerloop;".

outerloop:
for(i=0;i<a.length;i++){
    for(j=0;j<b.length;j++){
        if(b[j] === a[i]){
            continue outerloop;
        }
    }
    // if we get to here, then no value
    // of b[] matched the a[] value, so:
    // "do something"
}



第三种可能性,如果 元素 a [] b [] 中的任何地方找到。为此,您将检查 a [] 的每个元素的值,并将该值与 <$ c的所有元素进行比较$ c> b [] ,一个接一个,如果在检查 b [] 你找不到与 a [] 当前元素的值匹配,你设置了一个标志并使用 break outerloop; 退出所有循环。然后在最后,如果标志是设置,你可以做某事。


A 3rd possibility, is that you want to "do something", only one time, if any element of a[] can't be found anywhere in b[]. For this, you would examine the value of each element of a[], and compare that value to all elements of b[], one by one, and if after checking the value of all elements in b[] you couldn't find a match to the value of the current element of a[], you set a "flag" and exit all the loops using "break outerloop;". Then at the end, if the flag is "set", you can "do something".

bflag=false;
outerloop:
for(i=0;i<a.length;i++){
    for(j=0;j<b.length;j++){
        if(b[j] === a[i]){
            continue outerloop;
        }
    }
    // if we get to here, then no value
    // of b[] matched the a[] value, so, 
    // set the flag (bflag)
    bflag=true;
    break outerloop:
}

if(bflag){
    // "do something"
}

如果将此代码作为函数调用,可以简化一下:

If this code was called as a function, it could be simplified a bit:

outerloop:
for(i=0;i<a.length;i++){
    for(j=0;j<b.length;j++){
        if(b[j] === a[i]){
            continue outerloop;
        }
    }
    // if we get to here, then no value of b[] matched 
    // the a[] value, so, do-something and return "failed":
    // "do something"
    return false;
}

// all elements of a[] were matched to 
// elements of b[], so: return "success"
return true;

这篇关于JavaScript - 比较两个数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆