@null和往常一樣正常,我只是添加了這個例子來實現大數字。 您也可以在一個循環中初始化這一點,所以你能夠讓你想要的東西(如空建議你)......
var myArray:Array = ["a", "b", "c"];
var mySecondArray:Array = ["a", "b", "c"];
var myValues:Array = ["first element", "secondt element", "third element"];
var relValues:Array = [150, 75, 500];
var indexedValues:Array = [1, 2, 3];
var myObject:Object;
var mySecondObject:Object;
function converArraytToObject(myArray:Array, myValues:Array):Object{
var o:Object=new Object();
for(var i:uint = 0; i < myArray.length; i ++){
o[myArray[i].toString()] = myValues[i].toString();
}
return o;
}
trace(myObject["a"]);
// outputs "first element"
trace(myObject["b"]);
// outputs "second element"
trace(myObject["c"]);
// outputs "third element"
// or
trace(" ***UNSORTED");
for (var j in myObject){
trace ("index name = [" + j + "] = " +myObject[j]);
}
// or
trace(" ***SORTED by index");
for (var k = 0 ; k < myArray.length ; k++){
trace ("index name = [" + myArray[k] + "] = " + myObject[myArray[k]]);
}
//or
trace(" ***UNSORTED");
for (var l in mySecondObject){
trace ("index name = [" + l + "] = " + mySecondObject[l]);
}
//or
trace(" ***SORTED by index");
for (var m = 0 ; m < myArray.length ; m++){
trace ("index name = [" + myArray[m] + "] = " + mySecondObject[myArray[m]]);
}
OUTPUT:
如果你想存儲的同一實例一個類,我建議你使用Vector類來代替Array。
first element
secondt element
third element
***UNSORTED
index name = [c] = third element
index name = [a] = first element
index name = [b] = secondt element
***SORTED by index
index name = [a] = first element
index name = [b] = secondt element
index name = [c] = third element
***UNSORTED
index name = [c] = 3
index name = [a] = 1
index name = [b] = 2
***SORTED by index
index name = [a] = 1
index name = [b] = 2
index name = [c] = 3
ActionScript 3 fundamentals: Arrays
ActionScript 3 fundamentals: Associative arrays, maps, and dictionaries
ActionScript 3 fundamentals: Vectors and ByteArrays
我不明白什麼是 「MyObj中[」 一 「] [」 B 「] [」 C「] [...] = 20「的意思。我從來沒有在AS3中看到過這種語法。也許這只是我,因爲我對AS3相當陌生,但那不是我見過的。我希望這會拋出各種各樣的錯誤。因爲你正在設置一個等於一個整數的對象。這可能是什麼意思?一個對象不能'='一個整數。或者你的意思是對象中有20個屬性?這很混亂 –
也許你的意思是把它寫成像myObj = {「a」:String,「b」:String,「c」:String,...「t」:String};' –
@NealDavis myObj是包含另一個對象(多層)的對象。 myObj [「a」]的值是一個Object,它包含關鍵字「b」和包含關鍵字「c」的另一個Object的值... 您可以將其視爲JSON格式: { 「一」:{ 「b」:{ 「C」:{ ... }} } } 工程 – zeroflaw