当前位置:网站首页>JS for循环 循环次数异常
JS for循环 循环次数异常
2022-07-05 11:48:00 【News777】
错误代码
const res = await getList({
});
for(let i = 0;i < 5 - res.data.list.length;i++){
res.data.list.push({
day : new Date(),
name : 'xxx'
})
}
错误原因
需求:res.data.list
数组满足length === 5
原因:虽然变量res
用const声明,但由于其是一个对象,并不影响对象内属性值的变化,由于res.data.list
不断在push
,所以它的length
是在不断变化的,因此循环次数不断减少,res.data.list.length
的长度并没有达到预期
解决
const res = await getList({
});
const len = res.data.list.length // 增加
for(let i = 0;i < 5 - len;i++){
// 改变
res.data.list.push({
day : new Date(),
name : 'xxx'
})
}
边栏推荐
- The ninth Operation Committee meeting of dragon lizard community was successfully held
- SET XACT_ABORT ON
- 多表操作-自关联查询
- 【主流Nivida显卡深度学习/强化学习/AI算力汇总】
- redis主从模式
- pytorch-线性回归
- 如何让你的产品越贵越好卖
- How can China Africa diamond accessory stones be inlaid to be safe and beautiful?
- Simply solve the problem that the node in the redis cluster cannot read data (error) moved
- redis集群中hash tag 使用
猜你喜欢
随机推荐
ACID事务理论
Pytorch training process was interrupted
redis的持久化机制原理
【 YOLOv3中Loss部分计算】
1. Laravel creation project of PHP
项目总结笔记系列 wsTax KT Session2 代码分析
POJ 3176 cow bowling (DP | memory search)
【Win11 多用户同时登录远程桌面配置方法】
SET XACT_ABORT ON
[loss functions of L1, L2 and smooth L1]
Reading notes of growth hacker
【SingleShotMultiBoxDetector(SSD,单步多框目标检测)】
[crawler] bugs encountered by wasm
一次生产环境redis内存占用居高不下问题排查
idea设置打开文件窗口个数
阻止浏览器后退操作
Pytorch weight decay and dropout
How can China Africa diamond accessory stones be inlaid to be safe and beautiful?
Ncp1342 chip substitute pn8213 65W gallium nitride charger scheme
【load dataset】