• js数组的迭代器方法(some、every、forEach、map、filter)


    数组操作之迭代器方法(这些方法可以对数组中的每一个元素运用某个方法)

    一、不生成新数组的迭代器方法

    1、forEach方法(接收一个函数作为参数)

    function square(num){
            alert(num * num) ;
        }
        var nums = [1,2,3,4,5];
        console.log(nums.forEach(square));

    2、every方法(接收一个返回值为布尔值的函数,对数组中的每一个元素使用该函数)

    function isEven (num) {
            return num % 2==0;
        }
        var evens=[2,4,6,8,10];
        var isAllEven = evens.every(isEven);  //当且仅当数组中的所有的元素返回为真,every函数才会返回为真
        if(isAllEven){
            alert("all is even");
        }else{
            alert("not all is even");
        }

    3、some方法(只要数组中有一个元素使用方法返回为真,some函数就会返回真)

    var nums = [1,2,3,5,7,9];
            function isEven (num) {
            return num % 2==0;
            }
            alert(nums.some(isEven));   //true

    4.reduce方法

      用法①:为数组中的元素求和

    function add (currentTotal,currentValue) {
                    //alert(currentValue);
                    //alert(currentTotal);
                    return currentTotal+currentValue;
                }
                var nums = [1,2,3,4,5,6,7];
                alert(nums.reduce(add));

      用法②:将数组中的元素连接成一个长的字符串  

    function linkStr (foreStr,currentStr) {
                    return foreStr + currentStr;
                }
                var strings = ["nolan","is","studying","javascript"];
                alert(strings.reduce(linkStr));

    二、生成新数组的迭代器方法

      1.map方法(类似于不生成新数组中的forEach方法)

      用法①:取数组中每个单词的第一个字母

    function firstAlpha (word) {
                return word[0].toUpperCase();
            }
            var words = ["nolan","is","studying","javascript"];
            var firstAlphas = words.map(firstAlpha);
            alert(firstAlphas.join(""));   //使用join可以去掉连接数组元素的逗号

     2.filter方法(类似于every方法)

    用法①:筛选出符合条件(即返回值为true)的元素 

    function isEven (num) {
                return num % 2==0;
            }
            var nums=[1,2,4,6,8,10];            
            var evens = nums.filter(isEven);
            alert(evens);
            //案例:随机生成一些成绩,挑选出大于六十分的成绩
            function pass (grade) {
                if(grade>60){
                    return true;
                }else{
                    return false;
                }
            }
            var grades = [];
            for(var i=0;i<20;i++){
                grades[i]=Math.floor(Math.random()*101);
            }
            alert(grades);
            var passedGrades=grades.filter(pass);
            alert(passedGrades);
  • 相关阅读:
    Hello TensorFlow 二 (GPU)
    Hello TensorFlow
    C/C++调用Golang 二
    C/C++调用Golang 一
    再谈cgo(转)
    Go语言第一深坑
    通过样式改变图片明暗度,不是透明度哦
    webpack vue-loader was used without the corresponding plugin. Make sure to include VueLoaderPlugin
    webpack打包报错Chunk.entrypoints: Use Chunks.groupsIterable and filter by instanceof Entrypoint instead
    [Vue warn]: You are using the runtime-only build of Vue where the template compiler is not available. Either pre-compile the templates into render functions, or use the compiler-included build.
  • 原文地址:https://www.cnblogs.com/7766evr/p/7620414.html
Copyright © 2020-2023  润新知