View Full Version : Woes Getting a Value from an Associative Array

10-18-2009, 04:07 PM

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):

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!

10-18-2009, 05:24 PM
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:

Output+= rsJSON;

is already a string. So once that is finished building up, we have essentially:

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:

<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);


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:

function getKeyWord(n){

to get any particular "keywords" value from it. Thus avoiding the potential pitfalls of eval().

What was Output before you did this:

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.

10-18-2009, 05:46 PM
Hi John!


You nailed it.

It was resultSet that I needed and this does it:

function getKeyWord(n){

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!

10-18-2009, 07:26 PM
Great! As long as your problem is solved, I don't need to see any page or any more code. Enjoy!