Results 1 to 4 of 4

Thread: Woes Getting a Value from an Associative Array

  1. #1
    Join Date
    Sep 2009
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Woes Getting a Value from an Associative Array

    Hi!

    I have found the library accessdb.js - very useful for doing record manipulations with MS Access.

    At one point I do the following (using the library just mentioned):
    Code:
    var SQL = "SELECT distinct keywords FROM library ORDER BY KEYWORDS";
    var resultSet = myDB.query(SQL);
    var rsJSON = myDB.query(SQL, {json:true});
    Output+= rsJSON;
    (the latter via WorkArea.innerHTML = Output; when it is time to produce the output).

    The result is this:
    [{"keywords":"adults"},{"keywords":"CSLP"},{"keywords":"damage"},{"keywords":"degree value"},{"keywords":"employment"},{"keywords":"international"},{"keywords":"interventions"},{"keywor ds":"KPI"},{"keywords":"need"},{"keywords":"PSE"},{"keywords":"rural"},{"keywords":"SFA"},{"keywords ":"success"},{"keywords":"surveys"}]

    My familiarity with javascript is limited (but definitely improving these days!). Can anyone point the way to how I would, say, grab the third keyword in this list? (or any particular one for that matter).

    I need to store it in a variable (not in the array). So far I have not succeeded. Part of the problem is likely that I'm not using the right names for the elements of the variable array.

    If you want to see the javascript library (and the key to how this array is formed, and the correct names for the elements of the array) it is here: http://sourceforge.net/projects/accessdb/files/ (version 9.2)

    Thanks very much!
    Last edited by Warvinlin; 10-18-2009 at 05:10 PM. Reason: Adding quote tags for code

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    29,167
    Thanks
    44
    Thanked 3,240 Times in 3,201 Posts
    Blog Entries
    12

    Default

    Your output appears to be a text string resembling an ordinary array (not an associative array) of objects (objects are associative arrays), yet each of these objects has only one element each. But in any event it has all been converted into one long string at this point, otherwise it couldn't be innerHTML of anything.

    I'm not sure when or how that conversion to a string occurs. But I can see that it is not the ordinary conversion an array would go through via type conversion (a process that happens automatically in javascript when you try to output an array to a data container like innerHTML that can only be a string value). If it were, there would be no opening and closing ([]) brackets, and the 'objects' contained within would have been converted to their primitive string type value ('object' or 'Object[Object]' depending upon browser).

    Also, the operation method by which Output is being built up (+=) can only be applied to strings or to numbers, and this is obviously not a number.

    All of this would seem to indicate that the value of Output here:

    Code:
    Output+= rsJSON;
    is already a string. So once that is finished building up, we have essentially:

    Code:
    Output = '[{"keywords":"adults"},{"keywords":"CSLP"},{"keywords":"damage"},{"keywords":"degree value"},{"keywords":"employment"},{"keywords":"international"},{"keywords":"interventions"},{"keywor ds":"KPI"},{"keywords":"need"},{"keywords":"PSE"},{"keywords":"rural"},{"keywords":"SFA"},{"keywords ":"success"},{"keywords":"surveys"}]';
    Now, as long as there are no characters within that string that could be interpreted as math operators, and also no characters that would break the flawless representation of an array containing objects as a string (like nested quotes meant to be interpreted as literal quotes in the array or objects) none of which your example Output has, we may use eval to convert it back into an actual array of actual objects and parse it on that basis, ex:

    Code:
    <script type="text/javascript">
    var Output = '[{"keywords":"adults"},{"keywords":"CSLP"},{"keywords":"damage"},{"keywords":"degree value"},{"keywords":"employment"},{"keywords":"international"},{"keywords":"interventions"},{"keywor ds":"KPI"},{"keywords":"need"},{"keywords":"PSE"},{"keywords":"rural"},{"keywords":"SFA"},{"keywords ":"success"},{"keywords":"surveys"}]';
    
    function getKeyWord(n){
     var pArr = eval(Output);
     alert(pArr[n]["keywords"]);
    }
    
    getKeyWord(3);
    </script>
    Which will alert:

    degree value

    the forth value (arrays are zero indexed, so it is element #3 counting from zero).

    Now, if possible, I would prefer to perform this sort of thing on the data before it became a string, thus eliminating the need for eval() which has the drawbacks already mentioned.

    Is there a point in your code where you have the actual array of objects, or a way to do it a little differently so that you would be producing the actual array of objects instead of a string representation of it?

    If so, we could snag that as the value of Output and just do:

    Code:
    function getKeyWord(n){
     alert(Output[n]["keywords"]);
    }
    to get any particular "keywords" value from it. Thus avoiding the potential pitfalls of eval().

    What was Output before you did this:

    Code:
    Output+= rsJSON;
    And is that (as appears to be the case due to the += operator) part of some loop?

    What's the value of resultSet? It may be the array I'm looking for here. I can't really tell without seeing the rest of your code, if any. I can also tell more if I can see the page in action, as I could find what resultSet contains.
    Last edited by jscheuer1; 10-18-2009 at 08:46 PM. Reason: spelling
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. #3
    Join Date
    Sep 2009
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Hi John!

    Amazing.

    You nailed it.

    It was resultSet that I needed and this does it:

    Code:
    function getKeyWord(n){
     alert(resultSet[n]["keywords"]);
    }
    getKeyWord(0);

    I would gladly send you my entire app if you want - I could post it on a domain I have for my little development attempts. I didn't supply all the code because I know people are very busy and to slog through my entire nightmare seemed asking too much. . .

    Thanks SO much for looking at this and for taking the time to explain your logic. This has been plaguing me for three solid days, trying this and trying that. You've made it so I can use ALL the functionality in the library at ACCESSdb.org .

    Again, my HUGE appreciation on this one, man!

  4. #4
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    29,167
    Thanks
    44
    Thanked 3,240 Times in 3,201 Posts
    Blog Entries
    12

    Default

    Great! As long as your problem is solved, I don't need to see any page or any more code. Enjoy!
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •