JavaScript - Compare two arrays

arrays, javascript

Solution

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.

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];

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.

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
        }
    }
}

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".

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 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;

Problem

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?

Original source

Related problems