zoukankan      html  css  js  c++  java
  • JavaScript的map循环、forEach循环、filter循环、reduce循环、reduceRight循环

    1、map循环(返回数组)

    let arr=[1,2,3,4];
    arr.map(function(value,key,arr){  //值,索引,数组(默认为选定数组)
        return item;  //如果没有return则返回一个全是undefined的数组,个数是索引+1(key+1)
    })  //返回一个数组
    

    2、forEach循环(进行操作)

    let arr=[1,2,3,4];
    arr.forEach(function(value,key,arr){  //值,索引,数组(默认为选定数组)
        return item;  //进行任何操作
    })  //只返回undefined
    

     3、filter循环(过滤)

    let arr=[1,2,3,4];
    arr.filter(function(value,key,arr){  //值,索引,数组(默认为选定数组)
        return false;  //根据true和false查看是否返回原数组
    })  //false返回[],true返回只原数组
    

    4、reduce循环(从头到尾)(迭代)

    let arr=[1,2,3,4];
    arr.reduce(function(fristValue,nextValue,key,arr){  //数组第一个值(第一次存在),数组的下一个值,索引,数组(默认为选定数组)
        return fristValue+nextValue;  //所有数值相加,一直迭代
    })  //返回return迭代的结果
    

    5、reduceRight循环(从尾到头)(迭代)

    let arr=[1,2,3,4];
    arr.reduceRight(function(fristValue,nextValue,key,arr){  //数组最后一个值(第一次存在),数组下一个值,索引,数组(默认为选定数组)
        return fristValue+nextValue;  //所有数值相加,一直迭代
    })  //返回return迭代的结果
    

      

  • 相关阅读:
    学习vue_01
    练习题 vue_01:
    测试
    django小结
    BBS_02day
    BBS 03day
    力扣(LeetCode)412. Fizz Buzz
    力扣(LeetCode)415. 字符串相加
    力扣(LeetCode)448. 找到所有数组中消失的数字
    力扣(LeetCode)453. 最小移动次数使数组元素相等
  • 原文地址:https://www.cnblogs.com/huangqiming/p/7467117.html
Copyright © 2011-2022 走看看