Why does it work only with data[0] and not just data??
httpRequest.onreadystatechange = function(){
if(this.readyState==4 && this.status==200){
user_data = JSON.parse(this.responseText);
document.getElementById("display").innerHTML = "Temp ="+user_data.data[0].temp+" C";
}
}
But writing the below code doesn’t work
httpRequest.onreadystatechange = function(){
if(this.readyState==4 && this.status==200){
user_data = JSON.parse(this.responseText);
document.getElementById("display").innerHTML = "Temp ="+user_data.data.temp+" C";
}
}
After all data array has only one index, if it had more than one index it would be understandable to mention the index in square brackets e.g data[0] or data[1] or data[2] etc.
But it only has one index, so why do we need to write data[0] ??