Tag Archives: loop

Web

For loop through an associative array

assocarr
To go through an associative array or object, you could use:

<script type='text/javascript'>
/* <![CDATA[ */
var data = {};
data['var1'] = 'value1';
data['var2'] = 'value2';
data['var3'] = 'value3';
console.log(data);

for (var prop in data) {
  if (Object.prototype.hasOwnProperty.call(data, prop)) { 
    console.log("prop: " + prop + " value: " + data[prop])
  }
}
/* ]]> */
</script>