0

I ran into a bit of pickle lately, I'm trying to compare these two arrays.

Array1 = ["Red","Green","Blue","Yellow","Black"];

Array2 = ["Green","Violet","Black","White"];

I want to know if all the values in Array2 are not in Array 1. So what I came up with was this:

for(var i:int=0;i<Array2.length;i++)
{
    if(Array1.indexOf(Array2[i]) == -1)
    {
        trace("No String found!")
    }
} 

Right now it gives me a trace every time it can't find a value. The issue I have is that I want it to perform the trace only if all the values in the Array2 are not in Array1.

Does any body have an idea?

2 Answers 2

2

The best method is to search until a value is found, then exit the loop. If a value isn't found, it will naturally exit and the flag will remain false. You then check to see if the flag is true/false and perform your actions accordingly.

Try this:

var found:Boolean = false;
for(var i:int = 0; i < Array2.length && !found; i++)
{
    found = Array1.indexOf(Array2[i]) == -1;
}

if (!found)
{
    trace("No String found!");
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thant's it! Thank you very much.
0

I want to know if all the values in Array2 are not in Array 1

do you mean all or any? for all you could use (read: complicate by) this casalib arrayUtil class: http://as3.casalib.org/docs/org_casalib_util_ArrayUtil.html#containsAll

This function compares if ALL values are present, there are other functions there too that might be of help.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.