【问题标题】:Create associative array with arrays as values创建以数组为值的关联数组
【发布时间】:2020-04-04 20:28:31
【问题描述】:

在 cs4 中,我试图创建一个关联数组,其中值是数组。这些数组只有两个元素,我想像这样调用这两个元素之一:

var array1:Array = [5, "Example String"]
var array2:Array = [7, "Example String 2"]
var associativeArray:Object = {a1:array1, a2:array2}

trace(associativeArray[a1[0]]) // Print out the value of the first element of the first array. Should print out 5

但是这不会打印出第一个元素。奇怪的是,如果你省略“[0]”,程序会像这样打印整个数组:“5,示例字符串”。

我将如何从关联数组中的数组中仅打印一个元素。

【问题讨论】:

    标签: actionscript-3 flash flash-cs4


    【解决方案1】:

    您的方括号访问运算符 [ ] 中的参数顺序错误。您需要使用正确的符号:

    // The whole collection.
    trace(associativeArray);
    
    // The collection element, square bracket notation.
    // The key MUST be a String.
    trace(associativeArray["a1"]);
    
    // The collection element, dot notation.
    trace(associativeArray.a1);
    
    // Access the element of collection element.
    trace(associativeArray["a1"][0]);
    trace(associativeArray.a1[0]);
    
    // WRONG. Access non-existent element of the collection.
    trace(associativeArray[a1[0]]);
    trace(associativeArray["a1"[0]]);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-12-24
      • 1970-01-01
      • 1970-01-01
      • 2017-11-14
      • 2020-03-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多