Dave's Brain

Browse - programming tips - javascript foreach enumerate

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>');
	}
What this info useful to you? You can donate to say thanks

Add a comment

Sign in to add a comment
Copyright © 2008-2012, 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.
Advertisements: