473,395 Members | 2,446 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

How to set an arbitrary object value?

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?
Jul 23 '05 #1
6 1491
Steve Neill wrote:
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?
Jul 23 '05 #2
sn****@mxlogic.com (Steve Neill) wrote in message news:<25**************************@posting.google. com>...
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
Jul 23 '05 #3
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
Jul 23 '05 #4
Steve Neill wrote:
<snip>
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.
Jul 23 '05 #5
Steve Neill wrote:
sn****@mxlogic.com (Steve Neill) wrote in message news:<25**************************@posting.google. com>...
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.

--
Grant Wagner <gw*****@agricoreunited.com>
comp.lang.javascript FAQ - http://jibbering.com/faq

Jul 23 '05 #6
Thanks Richard!

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

Steve

"Richard Cornford" <Ri*****@litotes.demon.co.uk> wrote in message news:<cl*******************@news.demon.co.uk>...
Steve Neill wrote:
<snip>
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.

Jul 23 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: John Reese | last post by:
Hello there. I've run into some missing functionality with HTTP Digest authentication in the 2.3 library and I was wondering if I'm just missing something. Missing functionality the first:...
9
by: Andrew | last post by:
It seems that in C#, given an instance "a" of some value type "A", one can pass a by reference to functions with signatures of either "void f(object obj)" or "void g(ref A obj)". But passing a into...
5
by: Honestmath | last post by:
Hello everyone, I'm using a root finding algorithm in a function that only takes as arguments a function pointer and two variables that represent guesses at the roots of the function. int...
8
by: Steve Neill | last post by:
Can anyone suggest how to create an arbitrary object at runtime WITHOUT using the deprecated eval() function. The eval() method works ok (see below), but is not ideal. function Client() { }...
16
by: ES Kim | last post by:
"A Book on C" explains a technique to use an arbitrary array index range. int* p = malloc(sizeof(int) * 10) - 1; This way, the allocated array can be accessed with index range 1 ~ 10, not 0 ~...
3
by: Bob Day | last post by:
Ok, I have done a lot of reading(of the newsgroup answers, help files and MSDN articles) of synclock. I understand what you are saying in the newsgroup, and it is very helpful. It does, however,...
12
by: Chadwick Boggs | last post by:
I need to perform modulo operations on extremely large numbers. The % operator is giving me number out of range errors and the mod(x, y) function simply seems to return the wrong results. Also,...
2
by: mshiltonj | last post by:
I'm trying to find the preferred python idiom for access arbitrary fields of objects at run time. For example, say I have an object the business code will do *something* with three arbitrary...
5
by: raylopez99 | last post by:
I have a form, Form6, that has a bunch of buttons overlaid on it. I want to be able to click on any arbitrary area of the form, and if that area of the form is overlaid by a button, I want to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.