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!!!!
It is working fine if the data provided are correct
let indexMap = {"A": 0, "B": 1};
this.ctx = [
{
"label" : "A",
"data" : [0,0,0,0,0]
},
{
"label" : "B",
"data" : [0,0,0,0,0]
},
];
let id = indexMap["A"];
console.log((id))
this.result = this.ctx[id].data;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.