如果以关联方式使用,则数组不可迭代

时间:2022-06-25 18:55:14

Hi when I have an array in actionscript

嗨,当我在actionscript中有一个数组

var arr : Array = new Array();
arr["fsad"] = 1;
trace(arr.length);

and now put an entry to it with an associative string and afterwards count the length I get a length of 0 but why? How can I iterate it now?

现在用一个关联字符串为它添加一个条目,然后计算长度我得到的长度为0,但为什么呢?我怎么能现在迭代它?

Thanks in advance

提前致谢

Sebastian

5 个解决方案

#1


Well, to quote the reference:

那么,引用参考:

Do not use the Array class to create associative arrays (also called hashes), which are data structures that contain named elements instead of numbered elements. To create associative arrays, use the Object class. Although ActionScript permits you to create associative arrays using the Array class, you cannot use any of the Array class methods or properties with associative arrays.

不要使用Array类来创建关联数组(也称为哈希),它们是包含命名元素而不是编号元素的数据结构。要创建关联数组,请使用Object类。尽管ActionScript允许您使用Array类创建关联数组,但您不能将任何Array类方法或属性与关联数组一起使用。

I'm not sure why AS3 still allows Arrays to be used associatively - perhaps they were worried about AS2 migration - but it's best avoided. So far as I know, built-in Array fixtures like length and pop() will simply ignore anything added with a a key that isn't an integer, but they might also behave unpredictably.

我不确定为什么AS3仍允许Arrays以关联方式使用 - 也许他们担心AS2迁移 - 但最好避免使用。据我所知,像length和pop()这样的内置数组灯会简单地忽略用一个不是整数的键添加的任何东西,但它们也可能表现得不可预测。

#2


What you want to accomplish is called a Dictionary i guess :)

你要完成什么被称为字典我猜:)

#3


There is a post describing the same problem as you mentioned

有一篇帖子描述了你提到的同样问题

How do I find the length of an associative array in ActionScript 3.0?

如何在ActionScript 3.0中找到关联数组的长度?

I hope that it will help

我希望它会有所帮助

#4


In JavaScript (which is a brother of ActionScript) using spidermonkey:

在JavaScript(使用ActionScript的兄弟)中使用spidermonkey:

var obj = new Object(); // {}
obj["foo"] = 1;
print(obj.__count__); => 1 // non-standard Gecko

var arr = new Array(); // []
arr.push(1);
print(arr.length); => 1

Use Array for arrays and Object for dictionaries. It's not like PHP where everything is done using the same type.

对阵列使用Array,对词典使用Object。它不像PHP那样使用相同的类型完成所有事情。

#5


you can create you own associative arrays using Proxy ... this will come at a high performance cost, but you can implement array access overriding getProperty and setProperty, and for each in and for in overriding nextNameIndex as well as nextValue and nextName respectively ... you can also implement Array's forEach, filter, map, any, every etc. methods, so it looks like a real Array from outside ... but you should only do that, in situations, where it is not performace critical or unevitable ...

您可以使用代理创建自己的关联数组...这将带来高性能成本,但您可以实现覆盖getProperty和setProperty的数组访问,以及分别覆盖nextNameIndex以及nextValue和nextName的每个in和for。你也可以实现Array的forEach,filter,map,any,every等方法,所以它看起来像是一个来自外部的真实数组......但是你应该只在那些不具备关键性或不可避免的性能的情况下这样做。 ..

greetz

back2dos

#1


Well, to quote the reference:

那么,引用参考:

Do not use the Array class to create associative arrays (also called hashes), which are data structures that contain named elements instead of numbered elements. To create associative arrays, use the Object class. Although ActionScript permits you to create associative arrays using the Array class, you cannot use any of the Array class methods or properties with associative arrays.

不要使用Array类来创建关联数组(也称为哈希),它们是包含命名元素而不是编号元素的数据结构。要创建关联数组,请使用Object类。尽管ActionScript允许您使用Array类创建关联数组,但您不能将任何Array类方法或属性与关联数组一起使用。

I'm not sure why AS3 still allows Arrays to be used associatively - perhaps they were worried about AS2 migration - but it's best avoided. So far as I know, built-in Array fixtures like length and pop() will simply ignore anything added with a a key that isn't an integer, but they might also behave unpredictably.

我不确定为什么AS3仍允许Arrays以关联方式使用 - 也许他们担心AS2迁移 - 但最好避免使用。据我所知,像length和pop()这样的内置数组灯会简单地忽略用一个不是整数的键添加的任何东西,但它们也可能表现得不可预测。

#2


What you want to accomplish is called a Dictionary i guess :)

你要完成什么被称为字典我猜:)

#3


There is a post describing the same problem as you mentioned

有一篇帖子描述了你提到的同样问题

How do I find the length of an associative array in ActionScript 3.0?

如何在ActionScript 3.0中找到关联数组的长度?

I hope that it will help

我希望它会有所帮助

#4


In JavaScript (which is a brother of ActionScript) using spidermonkey:

在JavaScript(使用ActionScript的兄弟)中使用spidermonkey:

var obj = new Object(); // {}
obj["foo"] = 1;
print(obj.__count__); => 1 // non-standard Gecko

var arr = new Array(); // []
arr.push(1);
print(arr.length); => 1

Use Array for arrays and Object for dictionaries. It's not like PHP where everything is done using the same type.

对阵列使用Array,对词典使用Object。它不像PHP那样使用相同的类型完成所有事情。

#5


you can create you own associative arrays using Proxy ... this will come at a high performance cost, but you can implement array access overriding getProperty and setProperty, and for each in and for in overriding nextNameIndex as well as nextValue and nextName respectively ... you can also implement Array's forEach, filter, map, any, every etc. methods, so it looks like a real Array from outside ... but you should only do that, in situations, where it is not performace critical or unevitable ...

您可以使用代理创建自己的关联数组...这将带来高性能成本,但您可以实现覆盖getProperty和setProperty的数组访问,以及分别覆盖nextNameIndex以及nextValue和nextName的每个in和for。你也可以实现Array的forEach,filter,map,any,every等方法,所以它看起来像是一个来自外部的真实数组......但是你应该只在那些不具备关键性或不可避免的性能的情况下这样做。 ..

greetz

back2dos