Click to See Complete Forum and Search --> : [RESOLVED] associative array not populating


Shawazi
08-26-2008, 01:22 PM
//el.id and type are passed in through function

var a = new Subscription(el.id, type);
Control.subscriptions[el.id] = a;
console.log(a);
console.log(Control.subscriptions);


This shows that `a` has a value, but console logging Control.subscriptions always returns a blank array.

The weird thing is if I do


//el.id and type are passed in through function

var a = new Subscription(el.id, type);
Control.subscriptions.push(a);
console.log(a);
console.log(Control.subscriptions);


it works perfect

Shawazi
08-26-2008, 01:45 PM
i simplified for testing and still no luck


Control.subscriptions = new Array();
Control.subscriptions["abc"] = 'what?';
Control.subscriptions["Def"] = 'is next?';
alert(Control.subscriptions); //alerts blank
alert(Control.subscriptions["abc"]); //alerts "what?"



Control.subscriptions = new Array();
Control.subscriptions[0] = 'what?';
Control.subscriptions[1] = 'is next?';
alert(Control.subscriptions); //alerts "what?, is next?"
alert(Control.subscriptions[0]); //alerts "what?"

Weedpacket
08-26-2008, 09:18 PM
JavaScript doesn't have associative arrays.

JavaScript arrays are objects: foo['bar'] creates a new object property "bar" in the "foo" object (foo["bar"] and foo.bar mean the same thing).

JavaScript's inbuilt Array() object contains special rules to skip over non-numeric properties when it's being treated like an array (so that all its other properties like "length" and "join" don't show up when you iterate over it).

So, it looks like you don't actually want an Array here. Just use an ordinary object.

Shawazi
08-27-2008, 10:15 AM
Wow that's awesome I had no idea. Thanks.