AS3在数组中查找公共值

时间:2011-12-13 15:26:45

标签: arrays flash actionscript-3

我正在努力做一些不应该太难的事情,但我无法弄清楚我有多个具有不同值的数组,我想找到所有数组的常用值,请参阅下面的示例:

        var arrayOne:Array      = ["1","2","3"];
        var arrayTwo:Array      = ["1","2","7"];
        var arrayThree:Array    = ["1","2","9","12"];

        _resultArray = ["1","2"];

感谢任何帮助。

3 个答案:

答案 0 :(得分:3)

您可以执行以下操作:

///Returns common values between to arrays
function getCommonValues(array1:Array, array2:Array):Array
{
    var len1:int = array1.length;
    var len2:int = array2.length;
    var toReturn:Array = new Array();

    for(var i:int = 0; i < len1; i++){
        for(var n:int = 0; n < len2; n++){
            if(array1[i] == array2[n]){
                toReturn.push(array1[i]);
            }
        }
    }
    return toReturn;
}

然后执行以下操作:

var arrayOneAndTwo:Array = getCommonValues(arrayOne,arrayTwo);
var _resultArray:Array = getCommonValues(arrayOneAndTwo,arrayThree);

您可以选择修改函数以在比较中包含所有三个数组,这样会更有效。

修改

如果您想处理未知数量的数组,可以添加:

///Returns common values between X number of sub arrays
function getCommonValuesFromSubArrays(papaArray:Array):Array
{
    if(papaArray.length < 2){ return papaArray; }

    var toReturn:Array = papaArray[0];

    for(var a:int = 1; a < papaArray.length; a++){
        toReturn = getCommonValues(toReturn, papaArray[a]);
    }

    return toReturn;
}

然后像:

var arr1:Array = ["one","two","three","four","five"];
var arr2:Array = ["one","two","five","six"];
var arr3:Array = ["one","two","three","four","five"];
var arr4:Array = ["one","two","three","four","five"];

var bigOlArray:Array = [arr1,arr2,arr3,arr4];

var _results:Array = getCommonValuesFromSubArrays(bigOlArray);

答案 1 :(得分:3)

我会使用一个函数来连接所有数组,按数值排序,并收集所有可用项目的次数与作为参数传入的数组的次数完全相同:

var arrayOne : Array = [ "1", "2", "3" ];
var arrayTwo : Array = [ "1", "2", "7" ];
var arrayThree : Array = [ "1", "2", "9", "12" ];
// you can pass in any number of Arrays
trace ( searchArraysForCommonItems ( arrayOne, arrayTwo, arrayThree ) ); // returns ["1", "2"]


function searchArraysForCommonItems ( ...args : * ) : Array
{
    var searchArray : Array = [];
    for each ( var arr:Array in args)
        searchArray = searchArray.concat ( arr );

    var resultArray : Array = [];
    var last : String;
    var times : int = 0;
    for each ( var str : String in searchArray.sort ( Array.NUMERIC ))
        if (last == str) times++;
        else
        {
            if (times == args.length) resultArray.push ( last );
            last = str;
            times = 1;
        }

    return resultArray;
}

当然,您可以(并且应该)尽可能使用Vector.<String>代替数组来提高性能,但始终要记住Array.sort()是本机函数而非常快...

答案 2 :(得分:2)

我会使用Array.filter()函数来实现这个目的:

var _resultArray:Array = arrayOne.filter(
   function(item:String, index:int, arr:Array):Boolean
   {
      return (arrayTwo.indexOf(item) != -1 && arrayThree.indexOf(item));
   }
);

这将遍历arrayOne并返回一个数组,其值也出现在arrayTwo和arrayThree中。

编辑:这是一个函数,它将接受任意数量的数组并返回公共值:

function getCommonValues(arrayOne:Array, ... arrays:Array):Array
{
    var _resultArray:Array = arrayOne.filter(
       function(item:String, index:int, arr:Array):Boolean
       {
          return arrays.every(
            function (a:Array, index2:int, arr2:Array):Boolean
            {
                return a.indexOf(item) != -1;
            }
          );
       }
    );
    return _resultArray;
}

用法:

resultArray = getCommonValues(arrayOne, arrayTwo, arrayThree, arrayFour); 

该函数在第一个函数中有另一个嵌套闭包,所以可能有点难以理解,但我测试了它,它可以工作。