I'm having trouble creating a loop that is referencing an array within an object within an array.
var data_dictionary = [{
"category": "Food",
"items": [
{
"object": "Apple",
"price": 2.21
},
{
"object": "Banana",
"price": 1.12
}
]
}, {
"category": "Goods",
"items": [
{
"object": "Lawnmower",
"price": 25.55
},
{
"object": "Bicycle",
"price": 12.21
}
]
}
]
I would like to run a loop that will return the # of "objects", but I'm running into problems
var result =[]
var temp = []
for (var i=0; i < data_dictionary.length; i++) {
for (var c=0; c < data_dictionary[i].items.length; c++) {
temp.push (data_dictionary[i].items[c].object)
}
}
result.push (temp.length)
The issue has to do with calculating the length in "c < data_dictionary[i].items.length", and likely in the following line as well.
Ideally, I would like result to be 4, since there are 4 objects.
Thanks for reading.
data_dictionaryis the array with only one element. Is this a typo or a mistake?