AS3 Flex中的排序对象/映射? [英] Sorted Object/Map in AS3 Flex?

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

问题描述

可以在Actionscript 3 Flex中的对象中对对象进行排序或者有某种顺序吗?我有一个像这样创建的对象:

  private var data:Object = new Object(); 

然后我继续放入一些键和值:

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

之后,我循环遍历对象:

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

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

解决方案

正如@helloflash在他的答案关于循环通过对象,并根据 Adob​​e定义的关联数组 (它是Object类的一个实例):


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


是正常的,你不能在你的情况下使用订单的钥匙(使用像这样的对象)。



如果你不需要键,你可以像@helloflash所说在他的答案中,使用简单的数组或向量。否则,您可以使用一组对象,如下所示:

  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);
}

//所有这三个循环给出:
// Key:someKeyOne,Value = 0
// Key:someKeyTwo,Value = 1337
// Key:someKeyThree,Value = 123

或者您可以使用Object,然后将其排序为数组如下:

  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); (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);
}
(tmp_array中的obj){
trace('Key:'+ obj.key +',Value ='+ obj.value);
}

函数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;
}

//所有这三个循环给出:
// Key:someKeyOne,Value = 0
// Key:someKeyTwo,Value = 1337
// Key:someKeyThree,Value = 123

希望能帮助您。

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;

After that I loop through the object:

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.

解决方案

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) :

An associative array, sometimes called a hash or map, uses keys instead of a numeric index to organize stored values. Each key in an associative array is a unique string that is used to access a stored value. An associative array is an instance of the Object class, which means that each key corresponds to a property name. Associative arrays are unordered collections of key and value pairs. Your code should not expect the keys of an associative array to be in a specific order.

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

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天全站免登陆