How to set an arbitrary object value?

S

Steve Neill

Here's an intersting problem...

I have a JS object that has a property: "fullname"...

myObject.client.name.fullname = "John Smith";

I also have a corresponding DOM element with an "id" value set to
"client_name_fullname":

<input type="text" id="client_name_fullname">

When the DOM element detects an "onkeyup" event I wish to change the
JS object property value accordingly.

If hard-coded, I might express this change as:

myObject.client.name.fullname = { DOM element value }
or
myObject["client"]["name"]["fullname"] = { DOM element value }

However, if all I have is the DOM element id expressed as a string
value, how would I update the JS object value? Furthermore, I want to
do this WITHOUT resorting to the eval() function.

Any ideas?
 
R

Randy Webb

Steve said:
Here's an intersting problem...

I have a JS object that has a property: "fullname"...

myObject.client.name.fullname = "John Smith";

I also have a corresponding DOM element with an "id" value set to
"client_name_fullname":

<input type="text" id="client_name_fullname">

When the DOM element detects an "onkeyup" event I wish to change the
JS object property value accordingly.

If hard-coded, I might express this change as:

myObject.client.name.fullname = { DOM element value }
or
myObject["client"]["name"]["fullname"] = { DOM element value }

However, if all I have is the DOM element id expressed as a string
value, how would I update the JS object value? Furthermore, I want to
do this WITHOUT resorting to the eval() function.

Any ideas?

All objects are properties of the window object.

window[myObject[........]]


--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq
Answer:It destroys the order of the conversation
Question: Why?
Answer: Top-Posting.
Question: Whats the most annoying thing on Usenet?
 
S

Steve Neill

Here's an intersting problem...

I have a JS object that has a property: "fullname"...

myObject.client.name.fullname = "John Smith";

I also have a corresponding DOM element with an "id" value set to
"client_name_fullname":

<input type="text" id="client_name_fullname">

When the DOM element detects an "onkeyup" event I wish to change the
JS object property value accordingly.

If hard-coded, I might express this change as:

myObject.client.name.fullname = { DOM element value }
or
myObject["client"]["name"]["fullname"] = { DOM element value }

However, if all I have is the DOM element id expressed as a string
value, how would I update the JS object value? Furthermore, I want to
do this WITHOUT resorting to the eval() function.

Any ideas?


Expressed in another way... how could I simplify and make more generic
the following function:


function __setValue(value) {
var a = event.srcElement.id.split("_");

switch (a.length) {
case 1: myObject[a[0]] = value;
case 2: myObject[a[0]][a[1]] = value;
case 3: myObject[a[0]][a[1]][a[2]] = value;
case 4: myObject[a[0]][a[1]][a[2]][a[3]] = value;
case 5: myObject[a[0]][a[1]][a[2]][a[3]][a[4]] = value;
}
}


Regards,
Steve
 
S

Steve Neill

OK, so in that case how would you use that to solve this particular problem?

window[myObject["client.name.fullname"]] = x; ??? I don't think so!

I guess, I'm asking if the following code can be expressed more simply...

function __setValue(value) {
var a = event.srcElement.id.split("_");

switch (a.length) {
case 1: myObject[a[0]] = value;
case 2: myObject[a[0]][a[1]] = value;
case 3: myObject[a[0]][a[1]][a[2]] = value;
case 4: myObject[a[0]][a[1]][a[2]][a[3]] = value;
case 5: myObject[a[0]][a[1]][a[2]][a[3]][a[4]] = value;
}
}

Regards,
Steve
 
R

Richard Cornford

Steve Neill wrote:
Expressed in another way... how could I simplify and make
more generic the following function:

function __setValue(value) {
var a = event.srcElement.id.split("_");

switch (a.length) {
case 1: myObject[a[0]] = value;
case 2: myObject[a[0]][a[1]] = value;
case 3: myObject[a[0]][a[1]][a[2]] = value;
case 4: myObject[a[0]][a[1]][a[2]][a[3]] = value;
case 5: myObject[a[0]][a[1]][a[2]][a[3]][a[4]] = value;
}
}

var myObjects = {
client:{
name:{
fullname:'',
firstName:''
},
address:{
line1:'',
line2:'',
line3:'',
city:'',
country:''
}
},
other:{
name:{
fullname:'',
firstName:''
},
address:{
line1:'',
line2:'',
line3:'',
city:'',
country:''
}
}
};

function setObjectPropWithArrayOfPropNames(obj, ar, value){
var c = 0, d = (ar.length - 1);
while(
(c < d)&&
(obj = obj[ar[c++]])
);
if((c == d) && obj){
obj[ar[d]] = value;
return true; //success flag
}else{
return false; //failed (unresolved property name).
}
}

var test = 'client.name.fullname'.split('.');

setObjectPropWithArrayOfPropNames(myObjects, test, 'Randy Webb');

alert('myObjects.client.name.fullname = '+
myObjects.client.name.fullname);

setObjectPropWithArrayOfPropNames(myObjects, test,'Richard Cornford');

alert('myObjects.client.name.fullname = '+
myObjects.client.name.fullname);

test = 'other.address.city'.split('.');

setObjectPropWithArrayOfPropNames(myObjects, test, 'London');

alert('myObjects.other.address.city = '+myObjects.other.address.city);


Richard.
 
G

Grant Wagner

Steve said:
Here's an intersting problem...

I have a JS object that has a property: "fullname"...

myObject.client.name.fullname = "John Smith";

I also have a corresponding DOM element with an "id" value set to
"client_name_fullname":

<input type="text" id="client_name_fullname">

When the DOM element detects an "onkeyup" event I wish to change the
JS object property value accordingly.

If hard-coded, I might express this change as:

myObject.client.name.fullname = { DOM element value }
or
myObject["client"]["name"]["fullname"] = { DOM element value }

However, if all I have is the DOM element id expressed as a string
value, how would I update the JS object value? Furthermore, I want to
do this WITHOUT resorting to the eval() function.

Any ideas?

Expressed in another way... how could I simplify and make more generic
the following function:

function __setValue(value) {
var a = event.srcElement.id.split("_");

switch (a.length) {
case 1: myObject[a[0]] = value;
case 2: myObject[a[0]][a[1]] = value;
case 3: myObject[a[0]][a[1]][a[2]] = value;
case 4: myObject[a[0]][a[1]][a[2]][a[3]] = value;
case 5: myObject[a[0]][a[1]][a[2]][a[3]][a[4]] = value;
}
}

Regards,
Steve

The problem with this approach (aside from the fact that you should have a "break;" for each "case") is that if
you set:

myObject[a[0]] to a "value", you have overwritten the OBJECT that was there previously, that had the property
a[1], which was itself an object that had a property a[2] which was itself an object that had a property a[3],
etc.

In other words, setting myObject[a[0]] would result in the loss of all the other data in the property
hierarchy.

If you really want to set myObject[a[0]] to a value, then you need to do something like:

myObject[a[0]['X']['X']['X']['X'] = value;

I hope that's clear.
 
S

Steve Neill

Thanks Richard!

That was a great solution. I tweaked it a bit but in essence that
exactly fits my needs.

Steve

Richard Cornford said:
Steve Neill wrote:
Expressed in another way... how could I simplify and make
more generic the following function:

function __setValue(value) {
var a = event.srcElement.id.split("_");

switch (a.length) {
case 1: myObject[a[0]] = value;
case 2: myObject[a[0]][a[1]] = value;
case 3: myObject[a[0]][a[1]][a[2]] = value;
case 4: myObject[a[0]][a[1]][a[2]][a[3]] = value;
case 5: myObject[a[0]][a[1]][a[2]][a[3]][a[4]] = value;
}
}

var myObjects = {
client:{
name:{
fullname:'',
firstName:''
},
address:{
line1:'',
line2:'',
line3:'',
city:'',
country:''
}
},
other:{
name:{
fullname:'',
firstName:''
},
address:{
line1:'',
line2:'',
line3:'',
city:'',
country:''
}
}
};

function setObjectPropWithArrayOfPropNames(obj, ar, value){
var c = 0, d = (ar.length - 1);
while(
(c < d)&&
(obj = obj[ar[c++]])
);
if((c == d) && obj){
obj[ar[d]] = value;
return true; //success flag
}else{
return false; //failed (unresolved property name).
}
}

var test = 'client.name.fullname'.split('.');

setObjectPropWithArrayOfPropNames(myObjects, test, 'Randy Webb');

alert('myObjects.client.name.fullname = '+
myObjects.client.name.fullname);

setObjectPropWithArrayOfPropNames(myObjects, test,'Richard Cornford');

alert('myObjects.client.name.fullname = '+
myObjects.client.name.fullname);

test = 'other.address.city'.split('.');

setObjectPropWithArrayOfPropNames(myObjects, test, 'London');

alert('myObjects.other.address.city = '+myObjects.other.address.city);


Richard.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top