如何在另一个数组中拼接数组?
我正在尝试为我班上的孩子们制作游戏。某种琐事历史问题的创造者。如何拼接MasterArray以便在allEnglandArray中摆脱子阵列(Hard-England和Medium-England)。因为“MasterArray.splice()”似乎正在影响 - 只拼接allEngland数组或allFrance数组。但我需要摆脱那些子阵列......
我的代码:
var Easy-England:Array = ["item1","item2","item3","item4","item5"];
var Medium-England:Array = ["item6","item7","item8"];
var Hard-England:Array = ["item9","item10"];
var allEngland:Array = [Easy-England,Medium-England,Hard-England];
var Easy-France:Array = ["item11","item12","item13","item14","item15"];
var Medium-France:Array = ["item16","item17","item18"];
var Hard-France:Array = ["item19","item20"];
var allFrance:Array = [Easy-France,Medium-France,Hard-France];
// the list of countries goes on and on and on... (Italy, Hungary, etc.)
var allStuff:Array = [allEngland, allFrance, etc.];
var MasterArray:Array;
// FUNCTIONS
// clear MasterArray - first I clear out completely the MasterArray
function clearMasterArray():void
{
MasterArray.splice(0);
}
// update MasterArray - than I fill the MasterArray with data according to checkBoxes
function updateMasterArray():void
{
for (var i:int = 0; i<checkBoxBlock.myCheckBoxes.length; i++)
{
if (checkBoxBlock.myCheckBoxes[i].selected)
{
MasterArray.push(allStuff[i]);
}
}
}
// splice MasterArray - last thing I do is splice the items according to student's proficiency level, referred to as "studentPL".
function spliceMasterArray():void
{
if (studentPL == 1)
{
for (var i:int = 0; i<allStuff.length; i++)
{
allStuff[i].splice(5,5);
}
}
if (studentPL == 2)
{
for (var i:int = 0; i<allStuff.length; i++)
{
allStuff[i].splice(8,2);
}
}
if (studentPL == 3)
{
for (var i:int = 0; i<allStuff.length; i++)
{
trace("no need to splice");
}
}
}
然后我按照这个顺序在另一个函数中调用这些函数......
function creatorFunction():void
{
clearMasterArray();
updateMasterArray();
spliceMasterArray();
}
答案 0 :(得分:0)
而不是:
var allEngland:Array = [Easy-England,Medium-England,Hard-England];
试试这个:
var allEngland:Array = Easy-England.concat(Medium-England, Hard-England);
这样你就会有一个'扁平'数组(没有子数组),因此处理起来会更容易。