Iterating through object key value pairs with the forEach function

A simple example of iterating through object key value pairs with the forEach function.
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8"/>
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0">
    <meta name="description" content="Tizen Angular.js example"/>
    <title>Iterating through object with forEach example</title>

</head>
<body>

<script>

  var firstValue = [{'quantity':152, 'weight':42},{'quantity':122, 'weight':32},{'quantity':15, 'weight':2},{'quantity':52, 'weight':25}];

  Object.keys(firstValue).forEach(function (key) {
      document.write("<br>Object at index: " + key + ", quantity: " + firstValue[key]['quantity'] + ", weight: " + firstValue[key]['weight']);
  });

</script>
</body>

</html>

Responses

0 Replies