AS3将数组添加到另一个数组

时间:2014-09-10 19:07:55

标签: arrays actionscript-3

我的问题的例子。

 var array_1:Array = new Array();
 array_1[0] = [2,4,6,8];

 var array_2:array = new Array();
 array_2[0] = [10,12,14,16];
 array_2[1] = [18,20,22,24];

 // and the out come I want it to be is this  
 trace(array_1[0]) // 2,4,6,8,10,12,14,16,20,22,24

 // I did try  array_1[0] += array_2[0] but it didn't work currently   

任何建议都会很棒。

2 个答案:

答案 0 :(得分:0)

如评论中所述,您可以使用concat方法:

 var array_1:Array = new Array();
 array_1[0] = [2,4,6,8];

 var array_2:array = new Array();
 array_2[0] = [10,12,14,16];
 array_2[1] = [18,20,22,24];

 array_1[0] = array_1[0].concat(array_2[0]).concat(array_2[1]);

当然,这看起来非常混乱。我想知道你为什么要将数组存储在其他数组中而没有明显的原因。

答案 1 :(得分:0)

这将执行您要查找的内容,并允许您将多行数据添加到array_1array_2

var array_1:Array = new Array();
array_1[0] = [2,4,6,8];

var array_2:Array = new Array();
array_2[0] = [10,12,14,16];
array_2[1] = [18,20,22,24];

var combinedArray:Array = new Array();
for( var i:int = 0; i < array_1.length; i++ ) {
    combinedArray = combinedArray.concat(array_1[i]);
}
for( i = 0; i < array_2.length; i++ ) {
    combinedArray = combinedArray.concat(array_2[i]);
}

trace(combinedArray);