【问题标题】:Access field using string?使用字符串访问字段?
【发布时间】:2012-07-08 13:33:02
【问题描述】:

我有 ArrayCollection 的带有字段名称的字符串。

我想动态访问对象属性。

var myObject:MyObjectType = new MyObjectType();
var fields:ArrayCollection = new ArrayCollection(["f1", "f2", "f3"] );
for (var index:int = 0; index < (event.result as ArrayCollection).length; index++ ) {
    myObject.[fields[index].toString()] = event.result[index];
}

我该怎么做?

【问题讨论】:

    标签: string actionscript-3 dynamic field


    【解决方案1】:

    您应该将 for 循环中的行替换为以下内容:

    myObject[fields[index].toString()] = event.result[index];
    //      ^
    // Remove the dot
    

    另外,请注意fields 包含字符串,因此您可以删除.toString() 部分:

    myObject[fields[index]] = event.result[index];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-09-01
      • 2019-08-25
      • 1970-01-01
      • 1970-01-01
      • 2014-02-09
      • 1970-01-01
      • 1970-01-01
      • 2015-08-14
      相关资源
      最近更新 更多