If you are running version 7, you can use the toScript
function to pass cf variables to js. Also, each query column can be
treated as an array, but the syntax is tricky.
Those two bits of info will allow you to send the categoryid
and category fields to js as arrays. Then you just loop through one
of them to build your menu.