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

Mat*_*ard 10 apache-flex arrays actionscript-3

是否有一种简单的方法可以Object在ActionScript 3.0中检索关联数组的长度(以a实现)?

我知道在AS3中有两种主要的创建关联数组的方法:

  1. 使用Dictionary对象; 当钥匙不需要时,特别方便string
  2. 使用a Object,只需为每个所需元素创建属性.属性名称是键,值是,值.

我的应用程序使用方法#2(使用Object类来表示关联数组).

我希望有一些比我的for循环更本土的东西,它可以手动计算所有元素.

Dan*_*ark 9

你必须像你一样在for循环中计算它们.当然,你可以创建一个类并在该类中坚持for循环.

对于AS3中收藏的一些重要意义,请检查这些人.

编辑2013毫不奇怪,链接确实会打破时间.试试这个新的:http://www.grindheadgames.com/get-the-length-of-an-object.


Iai*_*ain 5

对此进行一些测试实际上让我感到惊讶。这是数组的正常使用:

var things:Array = [];
things.push("hi!");
trace(things.length);
// traces 1
trace(things);
// traces hi!
Run Code Online (Sandbox Code Playgroud)

这是如果我们将一个值设置为字符串:

var things:Array = [];
things["thing"] = "hi!";
trace(things.length);
// traces 0
trace(things);
// traces an empty string
trace(things["thing"]);
// traces hi!
Run Code Online (Sandbox Code Playgroud)

基本上,如果您使用字符串添加内容,您是在设置属性,而不是实际添加到数组中。让我想知道为什么 Array 以这种方式是动态的。

所以......是的,用 for ... in 循环计算项目!