Javascript-食物总数数组崩溃
Javascript - Food totals array crashing
我有一个食物总量的JS对象
foodData: [
{ biscuits: 2, cola:2, sandwiches:0, cake:0 },
{ biscuits: 2, cola:2, sandwiches:0, cake:0 },
{ biscuits: 2, cola:0, sandwiches:0, cake:0 },
{biscuits: 2, cola:0, sandwiches:0, cake:0 },
{biscuits: 2, cola:0, sandwiches:0, cake:0 },
{biscuits: 2, cola:0, sandwiches:4, cake:0 },
{biscuits: 0, cola:0, sandwiches:0, cake:4 }
],
我想做的是删除用户每天承诺的"不良食品"数量。最终计算节省的卡路里。
这些项目应该从第一个索引中删除,然后按顺序删除,所以如果他们需要删除2块食物,如果可能的话,它会删除一块饼干,然后是可乐,然后是三明治,然后是蛋糕。
他们可以去除5块食物,这样它就可以反复从每种食物中去除1块,然后再回到饼干中。这是计数应该做的
下面的代码可以工作,但在循环(任何超过3项的代码)时都会被卡住。我怀疑是if语句,或者可能是内部计数。任何关于如何避免陷入持续循环的建议。我想当条件满足时,打破if语句会有所帮助。
var array_of_functions = [this.totalMonday, this.totalTuesday, this.totalWednesday, this.totalThursday, this.totalThursday, this.totalFriday, this.totalSaturday, this.totalSunday];
for (i = 0; i < array_of_functions.length; i++) {
if (array_of_functions[i] > 0) {
count = 0;
do {
if (this.foodData[i]['biscuits'] > 0 && count < this.noRemovedFoods) {
this.foodData[i]['biscuits']--;
++count;
}
if (this.foodData[i]['cola'] > 0 && count < this.noRemovedFoods) {
this.foodData[i]['cola']--;
++count;
}
if (this.drinksData[i]['sandwiches'] > 0 && count < this.noRemovedFoods) {
this.drinksData[i]['sandwiches']--;
++count;
}
if (this.foodData[i]['cake'] > 0 && count < this.noRemovedFoods) {
this.dataData[i]['cake']--;
++count;
}
} while (count < this.noRemovedFoods)
}
}
如果您提供了更多的代码或适当的有意义的代码(例如,我不知道drinksData是什么),那就太好了。我假设你的代码在do-white循环中看起来像这样:
if (this.foodData[i]['biscuits'] > 0 && count < this.noRemovedFoods) {
this.foodData[i]['biscuits']--;
++count;
}
if (this.foodData[i]['cola'] > 0 && count < this.noRemovedFoods) {
this.foodData[i]['cola']--;
++count;
}
if (this.foodData[i]['sandwiches'] > 0 && count < this.noRemovedFoods) {
this.foodData[i]['sandwiches']--;
++count;
}
if (this.foodData[i]['cake'] > 0 && count < this.noRemovedFoods) {
this.foodData[i]['cake']--;
++count;
}
在这种情况下,您将陷入一个无限循环。问题出在你边做边循环的条件下。如果所有foodData项的位都为零,则会出现计数不会增加的情况,并且由于不满足条件count < this.noRemovedFoods
,您将陷入无限循环。
你可以做的一件事是,如果所有食物的位都为零,就打破"边做边做"的循环。如果我认为你的代码是我所理解的,那么重写上面的代码以打破do-white循环可能看起来像这样:
do {
if (this.foodData[i]['biscuits'] > 0 && count < this.noRemovedFoods) {
this.foodData[i]['biscuits']--;
++count;
}
if (this.foodData[i]['cola'] > 0 && count < this.noRemovedFoods) {
this.foodData[i]['cola']--;
++count;
}
if (this.foodData[i]['sandwiches'] > 0 && count < this.noRemovedFoods) {
this.foodData[i]['sandwiches']--;
++count;
}
if (this.foodData[i]['cake'] > 0 && count < this.noRemovedFoods) {
this.foodData[i]['cake']--;
++count;
}
if(this.foodData[i]['biscuits'] == 0 && this.foodData[i]['cola'] == 0 && this.foodData[i]['sandwiches'] == 0 && this.foodData[i]['cake'] == 0) {
break;
}
}
只要检查一下所有的食物位是否都变为零,就可以跳出边做边做的循环。
相关文章:
- 如何在映射数组中添加换行符
- javascript结合了数组和字典
- 需要帮助设置json数组
- 不能从angular2中的子组件指定父组件中的数组
- Javascript-食物总数数组崩溃
- Javascript筛选器数组抛出错误并崩溃
- 2d数组在ajax调用中崩溃
- 从 JSON(db 查询结果)加载大型 javascript 数组而不会使页面崩溃
- 合并两个有一定值的数组,在浏览器中崩溃
- 即使使用较小的块,Javascript数组性能也会崩溃
- Web工作程序在传递具有数组缓冲区的大型对象数组时会崩溃Chrome选项卡
- 序列化无符号整数(ulong)数组时,Json.NET崩溃
- 访问导致我的应用程序崩溃的数组元素
- 页面崩溃(可能是由于多维数组?)
- 为什么我的服务器崩溃时解析json数组
- 这个巨大的javascript数组从数据库加载崩溃我的网站
- 浏览器崩溃-复选框数组Ajax (PHP)
- 为什么javascript数组具有非常高的索引数会导致崩溃/变慢/麻烦
- 从字符串到整型的数组转换会导致浏览器崩溃
- 为什么 Javascript 数组在 php 中不会崩溃