Javascript: foreach

Die er niet is :P

Er is dus geen foreach in Javascript. Jammer want dit geeft meer obvious code:

String[] listNames = listViewNames.GetNames();

for(int x = 0; x < listNames.length; x++)
{
	String name = listNames[i];
	//do something with name
}

wordt...

String[] listNames = listViewNames.GetNames();

foreach(String name in listNames)
{
	//do something with name
}

Er is wel een andere syntax voor het for statement, wat lijkt op foreach

var listnames = listViewNames.GetNames();

for(var index in listNames)
{
	//i is now the index
	var name = listNames[index];
	//do something with name
}

Een extra stap dus, maar het kan handig zijn

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>