比较as3中的两个数组

时间:2016-04-27 06:33:40

标签: arrays actionscript-3 compare flash-cs6

我有两个数组。数组1具有预定义元素。数组两个在运行时收集元素。比较这两个数组后我需要得到true或false。这两个数组也可以是..

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

2 个答案:

答案 0 :(得分:1)

使用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;
}

答案 1 :(得分:0)

这很容易做到,我认为这个功能应该可行

//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")