0

几个小时前我发布了我的问题,但我想我想出了如何以更易于理解的方式提出我的问题。

这是我的代码:

// 1. Intro
var introPL1:Array = ["intro1","intro2","intro3","intro4"];
var introPL2:Array = ["intro5","intro6","intro7","intro8","intro9"];
var introPL3:Array = ["intro10","intro11"];
var introPL4:Array = ["intro12","intro13"];
var allIntro:Array = [introPL1,introPL2,introPL3,introPL4];
// 2. Clothes
var clothesPL1:Array = ["clothes1","clothes2","clothes3","clothes4","clothes5"];
var clothesPL2:Array = ["clothes6","clothes7","clothes8"];
var clothesPL3:Array = ["clothes9","clothes10"];
var clothesPL4:Array = ["clothes11","clothes12","clothes13"];
var allClothes:Array = [clothesPL1,clothesPL2,clothesPL3,clothesPL4];
// 3. Colored Numbers
var colNumPL1:Array = ["colNum1","colNum2","colNum3","colNum4","colNum5"];
var colNumPL2:Array = ["colNum6","colNum7","colNum8"];
var colNumPL3:Array = ["colNum9","colNum10"];
var colNumPL4:Array = ["colNum11","colNum12","colNum13"];
var allColNum:Array = [colNumPL1,colNumPL2,colNumPL3,colNumPL4];

var allStuff:Array;
allStuff = allIntro.concat(allClothes, allColNum);
trace(allStuff[4]);

当我跟踪 allStuff[4] 时,它显示“clothes1,clothes2,clothes3,clothes4,clothes5”。问题是,我希望所有的东西都在 allStuff 数组中(没有子数组),当我跟踪 allStuff[4] 时,我希望它显示“intro5”(巨大的 allStuff 数组中的第五项) .

4

2 回答 2

2

您要使用的功能是concat

这是adobe的示例

var numbers:Array = new Array(1, 2, 3);
var letters:Array = new Array("a", "b", "c");
var numbersAndLetters:Array = numbers.concat(letters);
var lettersAndNumbers:Array = letters.concat(numbers);

trace(numbers);       // 1,2,3
trace(letters);       // a,b,c
trace(numbersAndLetters); // 1,2,3,a,b,c
trace(lettersAndNumbers); // a,b,c,1,2,3

这很简单:

allStuff= allStuff.concat(introPL1,introPL2,introPL3,introPL4,clothesPL1,clothesPL2,clothesPL3,clothesPL4,colNumPL1,colNumPL2,colNumPL3,colNumPL4);

你也可以做一个

allStuff = []
for each(var $string:String in $arr){
   allStuff.push($string)
}

对于每个数组,或将其变成一个函数

于 2013-03-22T20:32:45.333 回答
0

好的,一旦你像这样声明了你的数组,你需要一个额外的操作来展平你的数组allClothes等等。这样做:

function flatten(a:Array):Array {
    // returns an array that contains all the elements 
    // of parameter as a single array
    var b:Array=[];
    for (var i:int=0;i<a.length;i++) {
        if (a[i] is Array) b=b.concat(flatten(a[i]));
        else b.push(a[i]);
    }
    return b;
}

它的作用:该函数首先创建一个空数组,然后逐个成员检查参数成员,如果第 i 个成员是一个数组,则以该成员作为参数调用自身,并将结果添加到其临时数组中,否则它只是将下一个成员推a入临时数组。因此,要使您allIntro的数组成为平面数组,请allIntro=flatten(allIntro)在声明它之后调用。其他数组也一样。

于 2013-03-23T06:46:38.407 回答