Reputation: 1194
Is there any way to implement a dictionary with ActionScript in a Flex application. For example, I want to store something like this.
public var orientation:ArrayCollection = new ArrayCollection([
{a: new Direction('0','0','b','0')},
{b: new Direction('a','c','0','h')},
{c: new Direction('0','b','d','e')},
{d: new Direction('c','0','0','0')},
{e: new Direction('f','g','0','c')},
{f: new Direction('0','0','e','0')},
{g: new Direction('0','0','0','e')},
{h: new Direction('0','b','0','i')},
{i: new Direction('l','h','j','m')},
{j: new Direction('i','k','0','0')},
{k: new Direction('0','0','0','j')},
{l: new Direction('0','0','i','0')},
{m: new Direction('0','i','0','0')}
]);
So that instead of
orientation.getItemAt(3).north
I could go something like
orientation.getItemAt('d')north
Without getting the following error
Implicit coercion of a value of type String to an unrelated type int.
Thank You for your help
Upvotes: 2
Views: 6367
Reputation: 90804
This is quite a strange structure you are using. Why not directly use a Dictionary
?
var orientation:Dictionary = new Dictionary();
orientation["a"] = new Direction('0','0','b','0');
orientation["b"] = new Direction('a','c','0','h');
// etc.
Then you can access the value directly by key - trace(orientation["a"])
.
If you really have to use the current structure, you could create a custom function to access items the way you want. Something like that should work (untested):
function getItemByKey(var array:ArrayCollection, var key:String):* {
for (var i:int = 0; i < array.length; i++) {
var item:* = array[i];
if (item[key]) return item;
}
return null;
}
Upvotes: 6