数组遍历六种方法
1. some (ck) ----- 某个一个为true,则为true ,且找到为true ,退出循环
对数组中每个元素执行一次ck函数,知道某个元素返回true,则直接返回true。如果都返回false,则返回false
检查整个数组中是否有满足ck函数的元素。
![](http://upload-images.jianshu.io/upload_images/5221572-1240f86df8b45a33.png)
result 当符合条件为true 退出循环
![](http://upload-images.jianshu.io/upload_images/5221572-65efca60a58df485.png)
log 日志
没有满足函数条件时 返回false
![](http://upload-images.jianshu.io/upload_images/5221572-49b3c04125400d94.png)
log 日志
2. every() -----某一个为false,则返回false
![](http://upload-images.jianshu.io/upload_images/5221572-0bd921c2cf6eb327.png)
循环返回 返回false 时 退出 并终止循环
![](http://upload-images.jianshu.io/upload_images/5221572-5e4a9dac2b0a1e70.png)
log日志
3. forEach () ---- 仅循环
![](http://upload-images.jianshu.io/upload_images/5221572-a8c784e9e2e3954b.png)
![](http://upload-images.jianshu.io/upload_images/5221572-cf4b12b662f33dac.png)
log日志
![](http://upload-images.jianshu.io/upload_images/5221572-be34894b9ebb56b1.png)
全部为true 时返回 true
4. map() ---- 返回每个元素返回值的集合
![](http://upload-images.jianshu.io/upload_images/5221572-cdbfd62aa67c049e.png)
![](http://upload-images.jianshu.io/upload_images/5221572-039d3790bcb90f0a.png)
log日志
5. filter() ----得到返回值为true的元素的集合 (过滤器)
![](http://upload-images.jianshu.io/upload_images/5221572-abba460a7ffc920a.png)
![](http://upload-images.jianshu.io/upload_images/5221572-b0ea1bc00fdda635.png)
log日志
没有符合条件时, 返回空数组
![](http://upload-images.jianshu.io/upload_images/5221572-1a405f4efd55d1bf.png)
log日志
6. reduce(ck,init ) -----依次执行ck(prv.next)
![](http://upload-images.jianshu.io/upload_images/5221572-c0b45cd780fe9321.png)
![](http://upload-images.jianshu.io/upload_images/5221572-fa5c9fb420bf453a.png)
init 为初始值,不设置时默认为数组下标为0 元素
![](http://upload-images.jianshu.io/upload_images/5221572-a04809fc2bbb51e0.png)
![](http://upload-images.jianshu.io/upload_images/5221572-0a3a1cf39a840688.png)
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步