How to set an arbitrary object value?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Steve Neill

    #1

    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_fu llname":

    <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?
  • Randy Webb

    #2
    Re: How to set an arbitrary object value?

    Steve Neill wrote:
    [color=blue]
    > 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_fu llname":
    >
    > <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?[/color]

    All objects are properties of the window object.

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


    --
    Randy
    comp.lang.javas cript 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?

    Comment

    • Steve Neill

      #3
      How to set an arbitrary object value?

      sneill@mxlogic. com (Steve Neill) wrote in message news:<25f8d8e5. 0410211427.2c99 c090@posting.go ogle.com>...[color=blue]
      > 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_fu llname":
      >
      > <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?[/color]


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


      function __setValue(valu e) {
      var a = event.srcElemen t.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

      Comment

      • Steve Neill

        #4
        Re: How to set an arbitrary object value?

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

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

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

        function __setValue(valu e) {
        var a = event.srcElemen t.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

        Comment

        • Richard Cornford

          #5
          Re: How to set an arbitrary object value?

          Steve Neill wrote:
          <snip>[color=blue]
          > Expressed in another way... how could I simplify and make
          > more generic the following function:
          >
          > function __setValue(valu e) {
          > var a = event.srcElemen t.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;
          > }
          > }[/color]

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

          function setObjectPropWi thArrayOfPropNa mes(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.fu llname'.split(' .');

          setObjectPropWi thArrayOfPropNa mes(myObjects, test, 'Randy Webb');

          alert('myObject s.client.name.f ullname = '+
          myObjects.clien t.name.fullname );

          setObjectPropWi thArrayOfPropNa mes(myObjects, test,'Richard Cornford');

          alert('myObject s.client.name.f ullname = '+
          myObjects.clien t.name.fullname );

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

          setObjectPropWi thArrayOfPropNa mes(myObjects, test, 'London');

          alert('myObject s.other.address .city = '+myObjects.oth er.address.city );


          Richard.


          Comment

          • Grant Wagner

            #6
            Re: How to set an arbitrary object value?

            Steve Neill wrote:
            [color=blue]
            > sneill@mxlogic. com (Steve Neill) wrote in message news:<25f8d8e5. 0410211427.2c99 c090@posting.go ogle.com>...[color=green]
            > > 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_fu llname":
            > >
            > > <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?[/color]
            >
            > Expressed in another way... how could I simplify and make more generic
            > the following function:
            >
            > function __setValue(valu e) {
            > var a = event.srcElemen t.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[/color]

            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 <gwagner@agrico reunited.com>
            comp.lang.javas cript FAQ - http://jibbering.com/faq

            Comment

            • Steve Neill

              #7
              Re: How to set an arbitrary object value?

              Thanks Richard!

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

              Steve

              "Richard Cornford" <Richard@litote s.demon.co.uk> wrote in message news:<clbfop$l1 g$1$830fa795@ne ws.demon.co.uk> ...[color=blue]
              > Steve Neill wrote:
              > <snip>[color=green]
              > > Expressed in another way... how could I simplify and make
              > > more generic the following function:
              > >
              > > function __setValue(valu e) {
              > > var a = event.srcElemen t.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;
              > > }
              > > }[/color]
              >
              > var myObjects = {
              > client:{
              > name:{
              > fullname:'',
              > firstName:''
              > },
              > address:{
              > line1:'',
              > line2:'',
              > line3:'',
              > city:'',
              > country:''
              > }
              > },
              > other:{
              > name:{
              > fullname:'',
              > firstName:''
              > },
              > address:{
              > line1:'',
              > line2:'',
              > line3:'',
              > city:'',
              > country:''
              > }
              > }
              > };
              >
              > function setObjectPropWi thArrayOfPropNa mes(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.fu llname'.split(' .');
              >
              > setObjectPropWi thArrayOfPropNa mes(myObjects, test, 'Randy Webb');
              >
              > alert('myObject s.client.name.f ullname = '+
              > myObjects.clien t.name.fullname );
              >
              > setObjectPropWi thArrayOfPropNa mes(myObjects, test,'Richard Cornford');
              >
              > alert('myObject s.client.name.f ullname = '+
              > myObjects.clien t.name.fullname );
              >
              > test = 'other.address. city'.split('.' );
              >
              > setObjectPropWi thArrayOfPropNa mes(myObjects, test, 'London');
              >
              > alert('myObject s.other.address .city = '+myObjects.oth er.address.city );
              >
              >
              > Richard.[/color]

              Comment

              Working...