# Defining an object with variable properties

**URL:** https://forum.kirupa.com/t/defining-an-object-with-variable-properties/35596
**Category:** flash
**Created:** [November 15, 2003, 9:25pm UTC](https://forum.kirupa.com/t/defining-an-object-with-variable-properties/35596 "2003-11-15T21:25:23Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![Voetsjoeba](https://avatars.discourse-cdn.com/v4/letter/v/ac91a4/32.png) [@Voetsjoeba](https://forum.kirupa.com/u/Voetsjoeba)
#### Post date: [November 15, 2003, 9:25pm UTC](https://forum.kirupa.com/t/defining-an-object-with-variable-properties/35596/1 "2003-11-15T21:25:23Z")

</div>

Hello everybody 🙂

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

```auto

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.

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [November 15, 2003, 9:30pm UTC](https://forum.kirupa.com/t/defining-an-object-with-variable-properties/35596/2 "2003-11-15T21:30:29Z")

</div>

what you posted doesn’t quite make sense 😛

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 🙂

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [November 16, 2003, 9:28am UTC](https://forum.kirupa.com/t/defining-an-object-with-variable-properties/35596/3 "2003-11-16T09:28:21Z")

</div>

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:

```auto

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:

```auto

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

```

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

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [November 16, 2003, 10:08am UTC](https://forum.kirupa.com/t/defining-an-object-with-variable-properties/35596/4 "2003-11-16T10:08:30Z")

</div>

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

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

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [November 16, 2003, 10:12am UTC](https://forum.kirupa.com/t/defining-an-object-with-variable-properties/35596/5 "2003-11-16T10:12:47Z")

</div>

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

```auto
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.

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [November 16, 2003, 10:14am UTC](https://forum.kirupa.com/t/defining-an-object-with-variable-properties/35596/6 "2003-11-16T10:14:30Z")

</div>

Nope, no go …

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [November 16, 2003, 10:18am UTC](https://forum.kirupa.com/t/defining-an-object-with-variable-properties/35596/7 "2003-11-16T10:18:23Z")

</div>

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

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [November 16, 2003, 10:20am UTC](https://forum.kirupa.com/t/defining-an-object-with-variable-properties/35596/8 "2003-11-16T10:20:38Z")

</div>

```auto
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 🙂

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [November 16, 2003, 2:11pm UTC](https://forum.kirupa.com/t/defining-an-object-with-variable-properties/35596/9 "2003-11-16T14:11:55Z")

</div>

Huh ? What are you doing there ? 😛
