I have an associative, mulitdimensial array abbreviated here that looks like:
I want to use this data and create the select options. However, I'm having difficulty having the "selected" written on the first option because I'm not sure how to let it know it's the first item (since length does not work on associative arrays). I thought of creating a counter of some sort, but don't know how to pull out the first one to do that. I've tried (unsuccessfully) doing something like:
or:
Is there a JS guru that understands how to accomplish this? Can it be done? Or do I have to scrap the associative arrays (choosen for a couple of reasons) and just go with an indexed array.
Thanks a million,
-f
Code:
var equations = new Array();
equations["Acre"] = ["Meter\u00b2" , "4.046 * Math.pow(10,3)"];
equations["Joule"] = ["Calorie (thermochemical)", "Electron Volt", "Erg", ".239", "1.8 * Math.pow(10,60)", "Math.pi()"];
I want to use this data and create the select options. However, I'm having difficulty having the "selected" written on the first option because I'm not sure how to let it know it's the first item (since length does not work on associative arrays). I thought of creating a counter of some sort, but don't know how to pull out the first one to do that. I've tried (unsuccessfully) doing something like:
Code:
while (j <= (var i in equations)) {;
if (j = 1)
{
document.write(<option selected="true">' + [i] + '</option>');
}
else
{
document.write("<option>" + [i] + "</option>");
}
j++
}
or:
Code:
for (var i in equations)
if ([i]==0) {document.write('<option selected="true">' + [i] + '</option>');}
else {document.write("<option>" + [i] + "</option>");}
Is there a JS guru that understands how to accomplish this? Can it be done? Or do I have to scrap the associative arrays (choosen for a couple of reasons) and just go with an indexed array.
Thanks a million,
-f