Dave's Brain

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>'); }

Add a comment

Sign in to add a comment
Copyright © 2008-2017, dave - Code samples on Dave's Brain is licensed under the Creative Commons Attribution 2.5 License. However other material, including English text has all rights reserved.