[thelist] tips

Peter Brunone (EasyListBox.com) peter at easylistbox.com
Wed Aug 25 22:08:30 CDT 2004


Diane,

	Thanks for the parseInt info; excellent tip.  I had that problem
a while back and found some crazy way around it; haven't seen it since,
but if I do, I'll be ready!


-----Original Message-----
From: thelist-bounces at lists.evolt.org
[mailto:thelist-bounces at lists.evolt.org] On Behalf Of Diane Soini

I don't often post any tips since I usually feel like everybody already 
knows more than I do. Recently I've worked with some folks who could 
benefit from these tips:
<tip type="Javascript arrays">
Most people understand javascript arrays as being indexed arrays. But 
javascript arrays can also be hashtables, with each element having a 
key that maps to a value. In an indexed array, the key is the number. 
In a hashtable, the key is something else.

A hashtable is very handy when, for example, you want to find unique 
names in a long list with duplicates. Iterate through the list and set 
each array element to something like myarray['Jane'] = 'Jane'; 
myarray['Mike'] = 'Mike'; As you iterate the list, duplicates will be 
cancelled out and you'll be left with only the unique items.

To retrieve them you can use a for-loop like you usually see one, or 
you can use a control structure like this:
for(var i in myarray){
	alert(i); //will tell you the key
	alert(myarray[i]); //will tell you the value
}
</tip>

<tip type="Javascript parseInt bug">
Tore my hair out trying to figure out why parseInt("08") and 
parseInt("09") didn't evaluate to  8 or 9 but instead evaluated to 0. 
Well it seems that the javascript engine is considering, in these two 
cases, that the number isn't a normal base 10 number like you would be 
expecting, but numbers from the octal system. "08" and "09" are invalid 
in this system, so parseInt returns 0.

To fix this, use parseInt(08,10) to ensure the function uses the base 
10 number system.

This is a bug in most browsers, but not all. Nevertheless, the fix will 
not hurt anything.
</tip>




More information about the thelist mailing list