Javascript for-in loop to iterate over keys of javascript object

for in loop over javascript associative array

<script type="text/javascript">
var a = {"k1":"v1", "k2":"v2"}; 

for (key in a) {
  if (a.hasOwnProperty(key)) {
    document.write(key + "=" + a[key] + "<br/>");
  }
}
</script>
refresh

for in loop over javascript DOM object

<script type="text/javascript">
var obj = window.screen;
for (key in obj) {
  document.write(key + "=" + obj[key] + "<br/>");
}
</script>
refresh
Share this article: share on Google+ share on facebook share on linkedin tweet this submit to reddit

Comments

Click here to write/view comments