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:
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:
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.
Bookmarks