I've got an array that holds a mix of objects and arrays that contain objects with the same properties
plantsArray = [
{type: "wheat", height: 20, output: 25},
{type: "soybeans", height: 30, output: 29},
[{type: "corn", height: 34, output: 9}, {type: "wheat", height: 20, output: 25}],
{type: "wheat", height: 20, output: 25},
..etc
]
Trying to take each individual object, look up the value of "output" and push that many more objects into a new array:
const harvestOutput = []
for (const plantItem of plantsArray) {
if (Array.isArray(plantItem) ) {
for (let i = 0; i < plantItem[i].output; i++) {
harvestOutput.push(plantItem[i])
}
}
else
for(let k = 0; k < plantItem.output; k++) {
harvestOutput.push(plantItem)
}
}
And getting error "cannot read property "output" of undefined" for that plantObj[i] reference... without suggesting others methods and solutions and such please and thanks (I know they're out there, this a school exercise!), am I missing something in either the syntax or the logic for accessing the value of the "output" property of those nested objects? The way I'm reading this, starting at the if statement, is "If plantItem is an array, set a variable i to 0, and as long as i is less than the value of the "output" property of the entry found at index position i of plantItem ...etc".
Rest of it executes fine.
Thanks!
**EDIT plantObj was a typo, fixed **