【问题标题】:Mapping to a Variable and retrieving once they match映射到变量并在匹配后检索
【发布时间】:2016-01-21 12:25:58
【问题描述】:

I have a list of check boxes that when selected the getEventName method should reurn the key that matches the label.例如,如果标签是“NEW”,则应该在 map[key] = “new” 时返回键。这些已在下面的 LABEL_EVENTTYPE 函数中定义。它总是返回一个空字符串,似乎无法弄清楚为什么。

public static const LABEL_EVENTTYPE_MAP:Object = {
        "CANCEL":["cancelled","expired", "doneForDay"],
        "NEW":["new"],
        "TRADE":["trade"],
        "AMEND":["replaced"],

}





private function getEventName(label:String):String{
            var map:Object = ReplayConstants.LABEL_EVENTTYPE_MAP;
            for each(var key:String in map){

                if (map[key] == label){
                    return key;
                }       

        }
            return "";
        }

【问题讨论】:

    标签: actionscript-3 apache-flex actionscript mapping flash-builder


    【解决方案1】:

    遍历对象属性需要for..in循环而不是for each.. in

    for (var key:String in map){
        if (map[key] == label){
           return key;
        }       
    }
    

    还要考虑到,地图中的对象是数组,这就是为什么您的比较 map[key] == label 将始终返回 false

    【讨论】:

      猜你喜欢
      • 2022-01-08
      • 2018-01-09
      • 1970-01-01
      • 1970-01-01
      • 2012-12-10
      • 1970-01-01
      • 1970-01-01
      • 2016-01-12
      • 2016-12-27
      相关资源
      最近更新 更多