I have following data
indexMap = {"A": 0, "B": 1};
self.ctx.chart.data.datasets = [
{
"label" : "A",
"data" : [0,0,0,0,0]
},
{
"label" : "B",
"data" : [0,0,0,0,0]
},
]
When I want to access data array of the first object of the datasets array,
I write the code below :
var idx = indexMap["A"];
var data = self.ctx.chart.data.datasets[idx].data; // <--- error occur [TypeError: Cannot read property 'data' of undefined]
But the error occurs, that says:
TypeError: Cannot read property 'data' of undefined
But the self.ctx.chart.data.datasets[idx] can print the value like below:
{
"label" : "A",
"data" : [0,0,0,0,0]
}
the variable self.ctx.chart.data.datasets[idx] really has the key 'data'
How do I fix it?
BTW, when I using the code below is no problem, why?
var idx = 0;
var data = self.ctx.chart.data.datasets[idx].data; // <--- this is ok!!! why!!!!
indexMap["A"]in the scope you are accessing it in. This error makes me suspect thatindexMapis not in scope.