Browse - Programming Tips - How can I enumerate all items in a javaScript associative array?

Date: 2009jun29
Language: javaScript
Level: beginner

Q.  How can I enumerate all items in a javaScript associative array?
Is there a foreach like some other languages?

A.  There is no foreach, this is how you do it in javaScript:

var a = { one: 1, two: 2, three: 3}; var key, val; for (key in a) { val = a[key]; document.writeln('a[' + key + ']=' + val + '<br>'); }