如何将两个不同的数组放在一个数组中

时间:2015-04-19 21:02:26

标签: arrays actionscript-3

我有两个不同的数组,名称和分数,我想将它们放在同一个数组中,因此它的[name:,score:]。

var allNames:Array = ["Test A", "Test B", "Test C"]; 
var allScores:Array = ["10","7","0"];**

例如,它采用每个数组的第一个元素,然后将它们放在一起,然后是第二个和第三个元素。所以上面的数组变成了

var NamesAndScore = [names:Test A score:10], [name:Test B score:7], [name:Test C score:0]

我很抱歉,因为我是初学者,这看起来很蠢。

1 个答案:

答案 0 :(得分:0)

var allNames:Array = ["Test A", "Test B", "Test C"]; 
var allScores:Array = ["10","7","0"];

// Initialise a new array for the names and scores
var namesAndScores:Array = [];

for (var i:int = 0; i < allNames.length; i ++)
{
    // Push an object with name and score properties onto the new array
    namesAndScores.push({
        name: allNames[i],
        score: parseInt(allScores[i], 10)
    });
}

// To access the name and score properties of the first element of the new array
trace("name 1", namesAndScores[0].name, "score 1", namesAndScores[0].score);