排序对象/地图在AS3的Flex? [英] Sorted Object/Map in AS3 Flex?

查看:127
本文介绍了排序对象/地图在AS3的Flex?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能进行排序的对象或有某种顺序在ActionScript 3中的Flex对象?我这是像这样创建一个对象:

Is it possible to sort an object or have some sort of order in an object in Actionscript 3 Flex? I have an object which is created like so:

private var data:Object = new Object();

我接下来要放一​​些键和值到它:

I then go on to put some keys and values into it:

this.data.someKeyOne = 0;
this.data.someKeyTwo = 1337;
this.data.someKeyThree = 123;

在通过对象,我循环:

for (var dataPart:String in this.data) {
    trace("Key: " + dataPart = " Value:" + this.data["dataPart"]);
}

但我的问题是,该数据不会被显示在我在初始化的顺序,这是令人沮丧的,因为它需要是在初始化的顺序。

But my issue is that the data will not be displayed in the order I initialized them in. This is frustrating as it is required to be in the order initialized.

推荐答案

由于@helloflash在<一说href="http://stackoverflow.com/questions/27668518/sorted-object-map-in-as3-flex/27668875#27668875">his回答有关通过对象循环,并且根据 Adob​​e的定义关联数组 (这是Object类的一个实例):

As @helloflash said in his answer about loop through an object, and according to Adobe's definition of associative array (which is an instance of the Object class) :

关联数组,有时也被称为散列或地图,使用键而不是数字索引来组织存储的值。在关联数组中的每个键是用于访问一个存储值的唯一字符串。关联数组是Object类,这意味着每个键对应属性名的一个实例。关联数组是键和值对的无序集合。您的code不应期望关联数组的键是在一个特定的顺序。

这是你得到的行为是正常的,你的情况,你不能得到有序的键(使用一个这样的对象)。

The behavior that you got is normal and you can not get ordered keys in your case (using an Object like that).

如果您不需要按键,你可以做@helloflash在<一说href="http://stackoverflow.com/questions/27668518/sorted-object-map-in-as3-flex/27668875#27668875">his回答,使用简单的Array或Vector。否则,你可以使用对象的数组,像这样的:

If you don't need keys, you can do as @helloflash said in his answer, using a simple Array or Vector. Otherwise you can use an Array of objects, like this :

var obj:Object, i:int, s:String;

var data_array:Array = [
        {key: 'someKeyOne', value: 0},
        {key: 'someKeyTwo', value: 1337},
        {key: 'someKeyThree', value: 123}
    ]
for(i = 0; i < data_array.length; i++){
    obj = data_array[i];
    trace('Key : ' + obj.key + ', Value = ' + obj.value);
}
for (s in data_array) {
    obj = data_array[s];
    trace('Key : ' + obj.key + ', Value = ' + obj.value);
}
for each (obj in data_array){
    trace('Key : ' + obj.key + ', Value = ' + obj.value);
}

// all these 3 loops give :     
//  Key : someKeyOne, Value = 0
//  Key : someKeyTwo, Value = 1337
//  Key : someKeyThree, Value = 123

或者您可以使用对象,然后排序成一个这样的数组:

Or you can use Object and then sort it into an Array like this :

var data_object:Object = new Object();
    data_object.someKeyOne   = {index:0, value: 0};
    data_object.someKeyTwo   = {index:1, value: 1337};
    data_object.someKeyThree = {index:2, value: 123};

var tmp_array:Array = sort_obj(data_object);

for(i = 0; i < tmp_array.length; i++){
    obj = tmp_array[i];
    trace('Key : ' + obj.key + ', Value = ' + obj.value);
}
for (obj in tmp_array) {
    obj = tmp_array[obj];
    trace('Key : ' + obj.key + ', Value = ' + obj.value);
}
for each (obj in tmp_array){
    trace('Key : ' + obj.key + ', Value = ' + obj.value);
}

function sort_obj(obj:Object):Array {
    var a:Array = [];
    for (var key:String in obj) {
        a.push({key: key, index: obj[key].index, value: obj[key].value});
    }
    a.sortOn('index', Array.NUMERIC);
    return a;
}

// all these 3 loops give :     
//  Key : someKeyOne, Value = 0
//  Key : someKeyTwo, Value = 1337
//  Key : someKeyThree, Value = 123

希望一切可以帮助你。

Hope all that can help you.

这篇关于排序对象/地图在AS3的Flex?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆