Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Is there a simple way to retrieve the length of an associative array (implemented as an Object) in ActionScript 3.0?

I understand that there are two primary ways of creating associative arrays in AS3:

  1. Use a Dictionary object; especially handy when the key does not need to be a string
  2. Use an Object, and simply create properties for each desired element. The property name is the key, and the value is, well, the value.

My application uses approach #2 (using the Object class to represent associative arrays).

I am hoping there is something more native than my for loop, which manually counts up all the elements.

like image 532
Matt Dillard Avatar asked Nov 12 '08 18:11

Matt Dillard


People also ask

How do you get the length of an associative Array in PHP?

If you want to find the length of an array in PHP, you can use the sizeof function and use the echo command along with it to print the length. The second method is using a function called: count().

Can we use Array in ActionScript?

In indexed arrays, each array element has an index, whereas in associative arrays, each array element has a name. In ActionScript, the Array class produces indexed arrays, while the Object class produces associative arrays.

Which method's you can use to create an Array in ActionScript?

In ActionScript 3 there are two ways to create an Array: with square brackets [] (also known as JSON syntax), and with the Array object.


4 Answers

You have to count them in a for loop as you do. Of course, you could make a class and stick the for loop in that class.

For some great implmentations of Collections in AS3, check these guys.

Edit 2013 Not surprisingly, links do break after time. Try this new one: http://www.grindheadgames.com/get-the-length-of-an-object.

like image 192
Dan Rosenstark Avatar answered Oct 14 '22 16:10

Dan Rosenstark


Doing a few tests on this has actually surprised me. Here's normal use of an Array:

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

Here's if we set a value to a string:

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

Basically if you add things using strings you're setting properties rather than actually adding to the array. Makes me wonder why Array is dynamic in this way.

So... yeah count the items with a for ... in loop!

like image 39
Iain Avatar answered Oct 14 '22 15:10

Iain


I think you're stuck with counting them "manually".

An option would be to wrap the whole thing in a class and keep a separate variable that you update as you add/remove.

like image 43
grapefrukt Avatar answered Oct 14 '22 15:10

grapefrukt


var count:int; 
var key:String; 

for (key in myObject)
{ 
    count++; 
} 

trace ("myObject has this many keys in it: " + count);

or, alternatively, the for-each syntax (I haven't tested to see which is faster)

for each (var o:* in myObject)
{
    count++;
}
like image 36
RickDT Avatar answered Oct 14 '22 15:10

RickDT