首页 > 其他分享 >跳出循环可不要再用forEach,map也不好用,不妨直接用for循环

跳出循环可不要再用forEach,map也不好用,不妨直接用for循环

时间:2023-07-11 11:44:20浏览次数:33  
标签:map return form 跳出 循环 forEach titleList

需求:循环一个数组保持请求顺序请求接口,且当前数组的值为1时,又需要异步请求另一个接口根据返回status值跳出本次循环。

解决思路:使用for循环,首先在循环中判断数组中值为1的,用async和await异步请求返回数据状态跳出循环;同时把符合条件的所有请求接口push到一个数组中去,最后Promise.all。

直接上代码:

 1 data() {
 2   return {
 3     dataList: [],
 4     form: {
 5         params: [],
 6         tableType: null,
 7         taskId: null
 8       },
 9     titleList: [9, 1, 2]
10   }  
11 }
12 methods: {
13    async getList() {
14       this.dataList = [];
15       const p = [];
16       // 循环调用同一个接口
17       for (let i = 0,l = this.titleList.length; i < l; i++) {
18         this.form.tableType = this.titleList[i];
19         if (this.titleList[i] === 1 || this.titleList[i] === 2) {
20           // 根据taskStatus值,跳出循环。
21           if (this.titleList[i] === 1) {
22             const { code, data } = await this.getInfo();
23             if( code !== 0 || data.taskStatus !== 1) {
24               break;
25             }
26           }
27         } else if (this.titleList[i] === 9) {
28           // 清空值。
29           this.form.params = [];
30         }
31         // 把符合条件的接口push到数组中去
32         p.push(querysNationalGrants(this.form));
33       }
34       Promise.all(p)
35         .then((res) => {
36           res.map((info) => {
37             if (info.code == 0) {
38               this.dataList.push(info.data) || [];
39               this.dataList.map((j) => {
40                 this.$set(j, "loading", false);
41                 return j;
42               });
43             }
44           });
45         })
46         .finally(() => {
47           this.loading = false;
48         });
49     },
50     getInfo(){
51        return getInfoById(this.form.taskId);
52     }
53 }

坑点:
1、当时还是来了就跳到forEach坑中去了,forEach不能跳出循环
2、然而呢又使用了some (return true 跳出整个循环)

1 let list = [1, 2, 3, 4, 5];
2 list.some((value, index) => {
3     if(value === 3){
4         return true;  //  当内部return true时跳出整个循环
5     }
6     console.log(value) // 1 2 
7 });

表面上看好像没什么毛病,但是到上面的判断处就是不起作用,纠结了很久。。。估计是我太肤浅了。

鉴定完毕,欢迎友们一起交流学习!!

标签:map,return,form,跳出,循环,forEach,titleList
From: https://www.cnblogs.com/liushihong21/p/17543538.html

相关文章

  • DevTools 无法加载源映射: 无法加载httplocalhost8081staticscssbootstrap.min.css.map
    DevTools无法加载源映射:无法加载http://localhost:8081/statics/css/bootstrap.min.css.map的内容:HTTP错误:状态代码404,net::ERR_HTTP_RESPONSE_CODE_FAILURE 解决办法:找到bootstrap.min.css,删除最后一行注释 注意:如果是css报错就删除:/*#sourceMappingURL=bootst......
  • C++面试八股文:用过std::set/std::map吗?
    C++面试八股文:用过std::set/std::map吗?某日二师兄参加XXX科技公司的C++工程师开发岗位第27面:面试官:用过std::set/std::map吗?二师兄:用过。面试官:能介绍一下二者吗?二师兄:std::set是一个有序的集合,其中的元素是唯一的,即每个元素只能出现一次。一般用于去重和自动排序。......
  • C#对象映射器Mapster
    1.前言    在开发中,我们经常用到对象之间的映射。谈到对象映射器,我们比较熟知的肯定是AutoMapper,但很少人会知道Mapster。今天在这里我们一起探讨一下什么是Mapster?为什么有了AutoMapper映射器了,还要学习使用Mapster?2.什么是Mapster?    Mapster是一个.NET库,它......
  • 离奇的std::map、std::set崩溃
    离奇的std::map、std::set崩溃现象描述定位之路1、和windows调用比较,没发现任何问题2、修改cmakelists.txt,发现也没有什么可以改的,能改的怎么改结果都一样3、最笨的办法之一用上,写一段这样的代码:4、面向互联网大法编程,百度、微软必应(不FQ,不能狗狗),发现一个帖子,缩小有问题代......
  • LWC 50:677. Map Sum Pairs
    LWC50:677.MapSumPairs传送门:677.MapSumPairsProblem:ImplementaMapSumclasswithinsert,andsummethods.Forthemethodinsert,you’llbegivenapairof(string,integer).Thestringrepresentsthekeyandtheintegerrepresentsthevalue.Ifthekey......
  • globalmapper加载DOM显示无法确定投影和datum
    Theprojection/datumofthisGeoTIFFfilecouldnotbedeterminedautomatically.Pleaseconfirmtheprojection/datumforthisfile.Checkwithyourdatasupplierforthisinformationifyoudonothaveit.选择loadfromfile,选择arcgis导出的某个矢量文件的.prj文......
  • mapbox_master
    1.项目描述根据奔跑吧面条的**vue-big-screen**开源框架基础上进行修改。项目需要全屏展示(按F11)。项目部分区域使用了全局注册方式,增加了打包体积,在实际运用中请使用按需引入。项目环境:Vue-cli、DataV、Echarts、Webpack、Npm、Node,axios,mock。请拉取master分支的代码,其......
  • 使用循环打包文件
    今天在工作中需要将很多文件打包成一个包,记录一下操作的过程:[root@openstackapplog]#files=()[root@openstackapplog]#foriin{10..14};dofiles+=("client_req-20230710-$i.log");done[root@openstackapplog]#forfilein"${files[@]}";dotar-zcvfclient_rep_lin......
  • HashMap 源码阅读
    HashMap源码阅读HashMap是线程不安全的,若需要考虑线程安全则需要用HashTable属性//默认大小1<<4为16staticfinalintDEFAULT_INITIAL_CAPACITY=1<<4;//最大2的30次方staticfinalintMAXIMUM_CAPACITY=1<<30;//默认负载因子0.75staticfinal......
  • Mapbox、GeoServer离线部署矢量地图
    Mapbox、GeoServer离线部署矢量地图关键词:Mapbox、GeoServer、Tomcat、PostgreSQL、PostGis一、地图数据获取使用OpenStreetMap获取中国的矢量地图数据二、安装GeoServer及VectorTiles扩展将下载好的GeoServer.war放入Tomcat,启动Tomcat后将VectorTiles扩展中的四个jar包放入GeoSe......