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:
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