Skip to content

JavaScript foreach JSON key-value | Example code

  • by

Use Object.keys() to get keys and the key will fetch a value from JSON object (data) in JavaScript foreach iteration.

Use index notation with the key.

Object.keys(obj).forEach(function(k){
    console.log(k + ' - ' + obj[k]);
});

JavaScript foreach JSON key-value

Simple example code used Object.keys() to get the keys array and use forEach() to iterate over them.

<!DOCTYPE html>
<html>
<body>

  <script>

    var data = {
      "VERSION": "1001.10",
      "JOBNAME": "EXEC_",
      "JOBHOST": "Loal",
      "LSFQUEUE": "80",
      "LSFLIMIT": "2022-10-27",
      "NEWUSER": "3"
    };

    Object.keys(data).forEach(function(key) {
      console.log('Key : ' + key + ', Value : ' + data[key])
    })

  </script>

</body>
</html> 

Output:

JavaScript foreach JSON key-value

Do comment if you have any doubts or suggestions on this JS JSON topic.

Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.

OS: Windows 10

Code: HTML 5 Version

Leave a Reply

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