« Schadenfreude | Main | CSS Reboot »
April 26, 2006
Got some Hash? Javascript associative arrays
First, the quick and dirty - creating an associative array in one fell swoop:
webstersCorrect = {
”color” : "colour",
“normalcy” : "normality",
"aloominum" : "aluminium",
"George W Bush" : "cocaine addict"
};
or even multi-dimensionally
Queen = {
”roger” : [”drums”,”blonde″],
“john” : [”bass”,”brown″],
“brian” : [”guitar, uke”,”black″],
“fred” : [”vox”,”black″]
}
Accessing elements of these is easy:
webstersCorrect["color"] = "Colour"
Queen["fred"] = ["vox","black"]
or even
Queen["brian"][0] = "guitar"
Programatically this is simplicty itself also:
var Result = "";
for ( Bit in webstersCorrect ) {
Result += Bit + " = " + webstersCorrect + "\n";
}
document.write( Result ); // yeuch!
would result in a list like this:
color = Colour
normalcy = normality
aloominum = aluminium
George W Bush = cocaine addict
you can also use nested loops
for ( Bit in Queen ) {
for ( Thing in Queen[Bit] ) {
Result += Queen[Bit][Thing];
... and whatever else...
}
}
Not very inspired but you get the idea...
They are great for ASP creating a HASH from the result of your SQL statement so that you can open your Recordset, fill your hash and then close the recordset. I have a load of modules built to help me do all these things so coding in ASP is nice a quick for me now, not to mention clean codewise as all the functions are in separate files off the page.
Of course ASP>NET does all that for you but we all know how much of a bloated pig that is...
Posted by dottie at April 26, 2006 11:14 PM