Number of Actionscript Object Objects

How can I get the number of properties in a common ActionScript object? (As the length of the array)

+7
source share
2 answers

You will need to iterate over all the elements to count them:

function objectLength(myObject:Object):int { var cnt:int=0; for (var s:String in myObject) cnt++; return cnt; } var o:Object={foo:"hello", bar:"world"}; trace(objectLength(o)); // output 2 
+20
source

An even shorter code is here:

 var o:Object={foo:"hello",bar:"world",cnt:2}; trace(o.cnt); // output 2; 

Remember to update the last argument in the list of objects if anything is added to it. I think that the main drawback of this approach. And now, of course, .cnt does not actually return the true length of the list, but rather, the length of the list is 1.

0
source

All Articles