Defining an object with variable properties

Hello everybody :slight_smile:

I’m facing a problem with defining objects. What I need to accomplish is this:


var cnms = this.cNames.split("|");
myDP = new Array({cnms[0]:"Chris", cnms[1]:"Priceless"}, {cnms[0]:"Nigel", cnms[1]:"Cheap"});

Obviously, this doesn’t work, otherwise I wouldn’t be typing this right now. How do I define an object with variable properties ? I’ve tried eval(), but it doesn’t work either.

what you posted doesn’t quite make sense :stuck_out_tongue:

so you have an array called cnms… what’s [font=couriernew]cnms[0]:“Chris”[/font], why do you try to reassign a value to it and at the same time declaring as a property of the object… :h: confusing :slight_smile:

I’m not trying to redefine the values of the array, I’m trying to define an object of which I want the properties to be a value from that array. You create an object like this:


newobject = {name:"MyName",age:"Yourage"};

Using the object initializers { and }. But instead of name and age as properties of the object (newobject.name, newobject.age), I want to use a value from the cnms array. So I tried defining the object like this:


newobject = {cnms[0]:"MyName",cnms[1]:"Yourage"};

But that doesn’t work. Neither does using eval(cnms[0]).

maybe,
[AS]
newobject = {this.cnms[0]:“MyName”,this.cnms[1]:“Yourage”};
[/AS]

If it doesn’t work feel free to laugh,
Shawn

I can’t do that because that code is inside an onLoad handler of a loadVars object.

lv.onLoad = function(){
var cnms = this.cNames.split("|");
myDP = new Array({cnms[0]:"Chris", cnms[1]:"Priceless"}, {cnms[0]:"Nigel", cnms[1]:"Cheap"});
}

If I’d use this.cnms[0], then the cnms array would have to be a property of the loadVars object, which it isn’t. I can however declare the array as property of the loadVars object … Let me try I out, though I doubt it’ll make a difference.

Nope, no go …

Well hey the loadvars thing wasn’t there before, now I see the problem, but, I don’t know how to fix it. Told you it would be a good laugh.
Good Luck,
Shawn

var cnmsR = this.cNames.split("|");
myDP = new Array(
{cnms:newArray(cnmsR[0], cnmsR[1])} ,
{cnms:newArray(cnmsR[2], cnmsR[3])}
}

I doubt it works, but give it a try anyways :slight_smile:

Huh ? What are you doing there ? :stuck_out_tongue: