Seeking help to understand object mutation during a for loop.
I anticipate that console.log(dish)
will display the dish
object with an updated ingredients
property containing an array of shifted ingredient
s.
However, when I check dish.ingredients
, it only displays the ingredients.
Surprisingly, logging dish
shows the dish objects without the ingredients.
What could be causing this behavior?
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
contains an array of dish
objects fetched from a mongoose query.