Trying to mutate objects in a for loop.
I am expecting console.log(dish)
to log the dish
object with an ingredients
property containing an array of unshifted ingredient
s.
When I log dish.ingredients
, it logs the ingredients.
When I log dish
, it logs the dish
objects without the ingredients.
Why is this?
for (let dish of dishArray) {
dish['ingredients'] = []
for (let ingredient of ingredientsArray) {
if (dish._id.equals(ingredient._dishID)) {
dish['ingredients'].unshift(ingredient)
}
}
console.log(dish['ingredients']) <-------------
console.log(dish) <-------------
}
dishArray
is an array of dish
objects returned from a mongoose query.