I'm trying to write a function that accesses an object inside of an array inside of an object and then push that into an array.
This is the code I have right now:
Javascript
stuff: function (index1, index2) {
for (var i = 1; i < index1.length; i++) {
state[index2].push(foodData[index1][i].name);
}
}
When I run storage.stuff('ingredientsToInclude', 'desired') I get the following error:
Cannot read property 'name' of undefined
However, if I access foodData["ingredientsToInclude"][1].name in the console it returns the correct value.
Not sure the reason for the discrepancy.
index1? Why are you using its length property as the limit for theforloop? Shouldn't that befooData[index1].length?iiteratesindex1, so you probably wantindex1[i]somewhere. TryfoodData[index1[i]]instead offoodData[index1][i]for (var i = 1; i < index1.length; i++) {tofor (var i = 1; i < foodData[index1].length; i++) {