Viraj Tharinda
Viraj Tharinda

Reputation: 29

compare two arrays in as3

I have two arrays. Array one has predefined elements. Array two collect elements at run-time.I need to get true or false after comparing these two arrays.Also these two arrays can be..

var ArrayOne:Array = new Array("b1","b2","b3","b4");
var ArrayTwo:Array = ["b1","b2","b3","b4"];
//output must be true 

var ArrayOne:Array = new Array("b1","b2","b3","b4");
var ArrayTwo:Array = ["b2","b1","b4","b3"];
//output must be true 

var ArrayOne:Array = new Array("b1","b2","b3","b4");
var ArrayTwo:Array = ["b1","b2","b3","b4","b1","b2"];
//output must be true 

var ArrayOne:Array = new Array("b1","b2","b3","b4");
var ArrayTwo:Array = ["b1","b2","b3","b4","b5"];
//output must be false

Upvotes: 0

Views: 224

Answers (2)

Yasuyuki  Uno
Yasuyuki Uno

Reputation: 2547

Use Array.indexOf(value)

function compareArrays(ArrayOne: Array, ArrayTwo: Array): Boolean{

    var ret:Boolean = true;

    for (var i:int=0; i<ArrayTwo.length; i++){
        var ind: int = ArrayOne.indexOf(ArrayTwo[i]);
        if (ind == -1){
            ret = false;
            break;
        }
    }

    return ret;
}

Upvotes: 1

Jeffin
Jeffin

Reputation: 1209

Thats easy to do , i think this functionality should work

//first create a copy of array 1
var temp:Array = ArrayOne.splice();

for(i:int = 0; i< temp.length; i++)
    {
        for(j:int = 0; j< arraytwo.length; j++)
        {
            if(temp[i]==arraytwo[j])
              {
                //remove the value from array
                temp.splice(thatvalue)
              }
        }
    }

 if(temp.length>0)
      trace("false")
 else
      trace("true")

Upvotes: 0

Related Questions