Creating an array of objects on the fly

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Claus Mygind
    Contributor
    • Mar 2008
    • 571

    Creating an array of objects on the fly

    I can't figure out how to build the array of objects from the string I return from an ajax call.

    Let's say I return a string:

    {name:"Claus", age:39};;{name: "Tom", age:42};;{name: "John",age: 13}

    This string is contained in the variable returnData.

    I have an array called jsData which I want to be a collection of objects and I would like the result to look like the following:

    jsData[0] = {name:"Claus", age:39}
    jsData[1] = {name:"Tom", age:42}
    jsData[2] = {name:"John",ag e:13}

    What I can't figure out is how to do that. I tried to use :

    var jsData = returnData.spli t(;;)

    I also tried to add [ ] to the beginning and end of the string that was returned to returnData, but that did not work either.

    I do not wish for "name" and "age" to be constants, they will vary with each ajax query as will the total number of records returned.

    I am trying to build a multi-purpose module that will display a table for me which I can use with my varied apps.
  • Claus Mygind
    Contributor
    • Mar 2008
    • 571

    #2
    I should have added that when I use the split option each letter in the string becomes an object in the jsData array like this.

    jsData[0] = {0:N}
    jsData[1] = {1:a}
    jsData[2] = {2:m}
    jsData[3] = {3:e}
    jsData[4] = {:}
    jsData[5] = {"}
    jsData[6] = {C}
    .
    .
    .

    Comment

    • acoder
      Recognized Expert MVP
      • Nov 2006
      • 16032

      #3
      [code=javascript]var jsData = returnData.spli t(";;");[/code]

      Comment

      • Claus Mygind
        Contributor
        • Mar 2008
        • 571

        #4
        I am not familiar with "Code: ( javascript )". I have tried to find it on the web with no luck. Could you please post an example of how it is used?

        Comment

        • Claus Mygind
          Contributor
          • Mar 2008
          • 571

          #5
          Well I finally got it to work. The secret (for me at least) was

          1. Proper formatting of the returned string from my ajax call '{<first row>};;{<next row>};;...'

          2. Using the "eval( )" method to turn the string into an object in the jsData array.

          see my example below.


          [HTML]<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
          <HTML>
          <HEAD>
          <SCRIPT LANGUAGE="JavaS cript">
          <!--
          function makeObjectFromS tring(string) {
          eval("var result = " + string);
          return result;
          }

          var returnedString = '{name:"Claus", age:39};;{name: "Tom", age:42};;{name: "John",age:13}' ;

          aString = returnedString. split(';;');

          jsData = new Array()

          jsData[0] = makeObjectFromS tring(aString[0]);

          for (var key in jsData[0])
          {
          alert(key);
          }

          //-->
          </SCRIPT>
          </HEAD>

          <BODY>

          </BODY>
          </HTML>[/HTML]
          Last edited by acoder; Apr 7 '08, 02:31 PM. Reason: Added code tags

          Comment

          • acoder
            Recognized Expert MVP
            • Nov 2006
            • 16032

            #6
            Originally posted by Claus Mygind
            I am not familiar with "Code: ( javascript )". I have tried to find it on the web with no luck. Could you please post an example of how it is used?
            Umm... that wasn't part of the code - it was just to show that the following code is JavaScript code.

            Comment

            • acoder
              Recognized Expert MVP
              • Nov 2006
              • 16032

              #7
              Originally posted by Claus Mygind
              Well I finally got it to work. The secret (for me at least) was

              1. Proper formatting of the returned string from my ajax call '{<first row>};;{<next row>};;...'

              2. Using the "eval( )" method to turn the string into an object in the jsData array.
              If you used JSON, one eval would be enough. Anyway, good to see that you've got a working solution.

              Comment

              • Claus Mygind
                Contributor
                • Mar 2008
                • 571

                #8
                That sounds like something which would speed up processing. I will try to research it as I have not heard of it before and don't know how to implement it.

                Thanks for the advice.

                Comment

                Working...