PDA

View Full Version : Using an ArrayCollection's/Object's key as a property


j.steele
09-09-2009, 08:50 PM
I have an object (or arraycollection) of arbitrary length.
ex.

var myAC:ArrayCollection = new ArrayCollection([
{foo: "bar", taco: "dan", beer: "stein"}
]);
var obj:Object = new Object();
obj[foo] = "bar";
obj[taco] = "dan";
obj[beer] = "stein";

Now, what I want to do is something like this:


for (var i:Number = 0; i < myAC.length; i++)
{
//apply each key/value pair to an external SWF generically, so I don't have to do switch statements
e.target.document.parameters.*myAC[i].key* = myAC[i].toString();
}

Thanks in advance,
Jason

wvxvw
09-09-2009, 09:29 PM
I would loop through the properties of the myAC[i][property] and assign them to
e.target.document.parameters[property]... But id you ask if there is a way to set multiple properties at once, then I guess there isn't.

j.steele
09-10-2009, 01:46 PM
that's what I'm trying to do, I'm just not getting it right.
Here's the code I'm working with:

//_currVO is a custom ValueObject
//e.target.parameters is an external SWF.
for(var i:Number = 0; i < _currVO.arguments.length; i++)
{
var foo:String = _currVO.arguments[i].key.toString();
e.target.parameters[_currVO.arguments[i].key.toString()] = _currVO.arguments[i].val.toString();
}

Anything jump out at you as wrong?

wvxvw
09-10-2009, 02:11 PM
well, I don't see anything called key or val in your setup... also, it's a strange way to do it - why won't you just use a generic Object, and loop through it's properties using for-in?

j.steele
09-10-2009, 03:01 PM
ok, I got it working.
The part that I had wrong was I was trying to apply properties to e.target.parameters, when it should have been e.target.document.parameters.

Sorry for the confusion, 'key' and 'val' were properties in my '_currVO' value object.

Thanks,
Jason.