zoukankan      html  css  js  c++  java
  • JS中数组的循环:map, some, every, forEach,each

    ~ 之前对于forEach方法了解的不多,在使用时,突然发现return true fasle break没有反应,仔细一看,还是大有文章,遂记……

    当想跳出循环可以使用every 和 some方法,下面是简单的总结

    every 当内部return false时跳出整个循环(return true;也是需要写)

    //every()当内部return false时跳出整个循环
    let list = [1, 2, 3, 4, 5];
    list.every((value, index) => {
        if(value > 3){
            console.log(value)// 4
            return false;
    
        }else{
            console.log(value)// 1 2 3
            return true;
        }
    
    });
    list.every((value, index) => {
        if(value > 3){
            console.log(value)
            return false;
    
        }else{
            console.log(value)// 1
            // return true;
            // 如果没有返回值true 的话,也会跳出循环
        }
    
    }); 

      forEach没有返回值,只针对每个元素调用func。

    // forEach没有返回值,只针对每个元素调用func。
        let list2 = [1, 2, 3, 4, 5];
        list2.forEach((value, index) => {
            if(value > 3){
                console.log(value)// 4 5
                return false;//没有返回值,ruturn false 仍向下执行
    
            }else{
                console.log(value)// 1 2 3
                return true;
            }
        });

    some 当内部return true时跳出整个循环

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

    map 有返回值,返回一个新的数组,每个元素为调用func的结果。

        let list5 = [1, 2, 3, 4, 5];
        let arr = [];
        arr = list5.map((value, index) => {
            return value * 2;
        });
        console.log(arr);//[2, 4, 6, 8, 10]

    jQuery有each循环,这个是异步的

    $(selector).each(function(index,element){});
    var arr = [['a', 'aa', 'aaa'], ['b', 'bb', 'bbb'], ['c', 'cc', 'ccc']]      
       $.each(arr, function(i, item){      
            $.each(item,function(j,val){
                alert(j);
                alert(val);
         }); 
    });
    var obj = { one:1, two:2, three:3};      
       each(obj, function(key, val) {      
            alert(key);   
            alert(val);      
       });   
    <input name="aaa" type="hidden" value="111" />
    <input name="bbb" type="hidden" value="222" />
    <input name="ccc" type="hidden" value="333" />
    <input name="ddd" type="hidden"  value="444"/>
    然后你使用each如下
     $.each($("input:hidden"), function(i,val){  
         alert(val);
         alert(i);
         alert(val.name);
         alert(val.value);   
     }); 

    ~ 之前对于forEach方法了解的不多,在使用时,突然发现return true fasle break没有反应,仔细一看,还是大有文章,遂记……
    当想跳出循环可以使用every 和 some方法,下面是简单的总结
    every 当内部return false时跳出整个循环(return true;也是需要写)//every()当内部return false时跳出整个循环let list = [1, 2, 3, 4, 5];list.every((value, index) => {    if(value > 3){        console.log(value)// 4        return false;
        }else{        console.log(value)// 1 2 3        return true;    }
    });list.every((value, index) => {    if(value > 3){        console.log(value)        return false;
        }else{        console.log(value)// 1        // return true;        // 如果没有返回值true 的话,也会跳出循环    }
    }); 12345678910111213141516171819202122232425forEach没有返回值,只针对每个元素调用func。// forEach没有返回值,只针对每个元素调用func。    let list2 = [1, 2, 3, 4, 5];    list2.forEach((value, index) => {        if(value > 3){            console.log(value)// 4 5            return false;//没有返回值,ruturn false 仍向下执行
            }else{            console.log(value)// 1 2 3            return true;        }    });12345678910111213some 当内部return true时跳出整个循环    let list3 = [1, 2, 3, 4, 5];    list3.some((value, index) => {        if(value === 3){            return true;//当内部return true时跳出整个循环        }        console.log(value)// 1 2     });1234567map 有返回值,返回一个新的数组,每个元素为调用func的结果。    let list5 = [1, 2, 3, 4, 5];    let arr = [];    arr = list5.map((value, index) => {        return value * 2;    });    console.log(arr);//[2, 4, 6, 8, 10]--------------------- 作者:Shuah153 来源:CSDN 原文:https://blog.csdn.net/weixin_36934930/article/details/81061063 版权声明:本文为博主原创文章,转载请附上博文链接!

  • 相关阅读:
    RabbitMQ 安装
    redis windows安装
    Python Scrapy 爬取煎蛋网妹子图实例(二)
    Python Scrapy 爬取煎蛋网妹子图实例(一)
    Python pymysql 增删改查封装
    Python Scrapy 爬虫框架实例(一)
    Mac Anaconda 简单介绍 -- 环境管理
    python Scrapy 常见问题记录
    Hive json字符串解析
    大数据
  • 原文地址:https://www.cnblogs.com/shan-pj/p/11277741.html
Copyright © 2011-2022 走看看