Working on looping through a set of data and adding the names of foods to one array. Current set up below:
Note: The allDBfoods is a collection of food data seeded in the database.
<% var breakfastFoods = [] %>
<% for (var food of allDBFoods) { %>
<% if (food.breakfast == true ){ %>
<% breakfastFoods.push(food.name) %>
<% console.log(breakfastFoods)%>
<% } %>
<% } %>
This works but returns
[ 'Eggs' ]
[ 'Eggs', 'Bacon' ]
[ 'Eggs', 'Bacon', 'poptarts' ]
I only want it to make one array. Any advice?
Try to use log outside of for loop:
<% var breakfastFoods = [] %>
<% for (var food of allDBFoods) { %>
<% if (food.breakfast == true ){ %>
<% breakfastFoods.push(food.name) %>
<% } %>
<% } %>
<% console.log(breakfastFoods)%>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With