View Full Version : Array.length == 0 for a filled Array!
Chuck Taylor
03-17-2007, 06:03 PM
var ar:Array = new Array();
ar['a'] = 'Ha';
ar['b'] = 'Durr';
trace('LEN: '+ ar.length); // 0
trace('CONT: ' + ar['b']); // Durr
Isn't the Array class suppose to keep track of items added to it?
Paul Ferrie
03-17-2007, 06:16 PM
var ar:Array = new Array();
ar[0] = 'Ha';
ar[1] = 'Durr';
array index should be a integer
Chuck Taylor
03-17-2007, 06:34 PM
You mean AS doesn't support Associative Arrays? I rememer reading somewhere that the Associative Array support in JavaScript is only apparent. In reality, it doesn't support Associative Arrays. And to demonstrate the fact they had used a code similar to mine, now I remember.
Probably it's the same in AS. With all these OOP, they should have incorporated support for Associative Arrays too.
var ar:Array = new Array();
ar['a'] = 'Ha';
ar['b'] = 'Durr';
basically creatse attributes on the array but they are not inserted into the array list itself.
cybergatuno
03-18-2007, 12:52 AM
You may simulate "associative arrays" by using objects:
var list:object = new Object ();
list['a'] = "haha";
list['b'] = "hoho";
for ( var name:String in list ) {
cybergatuno
03-18-2007, 12:53 AM
You may simulate "associative arrays" by using objects:
var list:object = new Object ();
list['a'] = "haha";
list['b'] = "hoho";
for ( var name:String in list ) {
trace ( name + " = " + list[name] );
}
hangalot
03-18-2007, 10:17 AM
i would recomend using the Dictionary class when making associative arrays.
senocular
03-18-2007, 08:09 PM
All dynamic objects in Flash (including arrays) function as "associative arrays" in that they can contain any number of values with a dynamic variable identifer (property name), a, b, your cat's name, whatever, with each of these properties enumerable through a for in loop.
Array objects, however, do not count non-numeric indexes within their length property. If you need those indexes/properties counted, you can use a for in loop to count each object accessible in the enumeration, manually keep count through a custom property, or make a subclass of Proxy with its own length property that will keep a counter for all definitions created within the object.
The only difference with Dictionary objects is that their indexes are not limited to strings (or numbers) but can instead also include object values. Other objects such as object Object or Arrays will convert non-string keys into strings.
hangalot
03-18-2007, 09:49 PM
as i recall a feature of the dictionary is weak refrencing as well.
Assertnfailure
03-19-2007, 01:58 AM
yeah but if he just wants to use strings, then that's not gonna help him
hangalot
03-19-2007, 09:29 AM
obviously you can use a string as well
senocular
03-19-2007, 04:06 PM
as i recall a feature of the dictionary is weak refrencing as well.
That's true; dictionaries variables and listeners are the only weak listeners available in AS.
|
vBulletin® v3.8.4, Copyright ©2000-2009, Jelsoft Enterprises Ltd.