I'm running a HTML file which comes up with an error "Uncaught TypeError: Cannot read property '0' of undefined"
My javascript code is
function loadHeatMap(){
xhr=new XMLHttpRequest();
xhr.open('GET','/LinkedIn/heatMap.json',true);
xhr.onreadystatechange=function(){
if(xhr.readyState===4&&xhr.status===200){
heatData=JSON.parse(xhr.responseText).data;
console.log("Heat Map JSON data");
}};
xhr.send(null);
}
loadHeatMap();
var name = heatData.set[0].name;
The JSON is structured like this
{"data":[{"set":[{"name":"Joe","sold":"100","target":"200"},{"name":"Tim","sold":"200","target":"100"}]}]}
The error occurs at the line 'heatData.set[0].name, saying that 0 is undefined.
Can someone please let me know what I'm doing wrong.