AS3 Flex 中的排序对象/地图? [英] Sorted Object/Map in 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 在 他的回答关于循环遍历一个对象,并根据 Adobe对关联数组
的定义(它是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 类的一个实例,这意味着每个键对应一个属性名称.关联数组是键值对的无序集合.您的代码不应期望关联数组的键按特定顺序排列.
你得到的行为是正常的,你不能在你的情况下得到有序的键(使用这样的对象).
The behavior that you got is normal and you can not get ordered keys in your case (using an Object like that).
如果你不需要钥匙,你可以按照@helloflash在他的答案,使用简单的数组或向量.否则,您可以使用对象数组,如下所示:
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
或者你可以使用 Object 然后像这样将它排序成一个数组:
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
希望能帮到你.
这篇关于AS3 Flex 中的排序对象/地图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!