I am currently working on iterating over nested objects and storing the outer object key along with the inner nested object values in arrays. My goal is to use this data to display them in a group bar chart using chart.js.
let goodArray = [];
let notgoodArray = [];
let fruitsArray =[];
const obj = {
"oranges": {
"good": 1,
"not_good": 0
},
"apples": {
"good": 1,
"not_good": 0
},
"grapes": {
"good": 2,
"not_good": 0
}
}
I have managed to store the outer object keys like this
for (var key in this.obj) {
fruitArray.push(key);
}
This results in the fruit array looking like this
['oranges', 'apples', 'grapes']
However, I am unable to access the properties of the fruit objects within this loop. I tried adding code like this
for (var key in this.obj) {
goodArray.push(key.good);
notgoodArray.push(key.not_good);
fruitArray.push(keyname);
}
But this resulted in an error message saying
"Property 'good' does not exist on type 'string'"
My aim is to have an array for the 'good' values like
[1, 1, 2]
and another array for the 'not_good' values like
[0, 0, 0]