Here the value of i
is temp, hum.
but when i want to put the value of i in " myObj.i[j];
" then it is giving me the error:
Uncaught TypeError: Cannot read property '0' of undefined at json_trial.html:21
while I am doing x=myObj.temp[j];
it is working fine.
I don't want to use it directly, I want to use it dynamically. Please help
<!DOCTYPE html>
<html>
<body>
<p>Access an array value of a JSON object.</p>
<p id="demo"></p>
<script>
var myObj, x;
myObj = {
"temp": ["Temperature", "22.8", "℃"],
"hum": ["Humidity", "48.8", "%"]
};
for (var i in myObj) {
alert(i);
for (var j = 0; j < 3; j++) {
x = myObj.i[j];
alert(x);
document.getElementById("demo").innerHTML = x;
}
}
//x = myObj._1[2];
//document.getElementById("demo").innerHTML = x;
</script>
</body>
</html>