gpt4 book ai didi

arrays - 遍历数组 - 只打印一次?

转载 作者:行者123 更新时间:2023-12-01 12:08:12 24 4
gpt4 key购买 nike

致力于循环遍历一组数据并将食物名称添加到一个数组中。当前设置如下:

注意:allDBfoods 是数据库中食物数据的集合。

<% var breakfastFoods = []  %>
<% for (var food of allDBFoods) { %>
<% if (food.breakfast == true ){ %>

<% breakfastFoods.push(food.name) %>
<% console.log(breakfastFoods)%>
<% } %>
<% } %>

这有效但返回

[ 'Eggs' ]
[ 'Eggs', 'Bacon' ]
[ 'Eggs', 'Bacon', 'poptarts' ]

我只想让它做一个数组。有什么建议吗?

最佳答案

尝试在for 循环之外使用log:

<% var breakfastFoods = []  %>
<% for (var food of allDBFoods) { %>
<% if (food.breakfast == true ){ %>
<% breakfastFoods.push(food.name) %>
<% } %>
<% } %>
<% console.log(breakfastFoods)%>

关于arrays - 遍历数组 - 只打印一次?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54553705/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com