zoukankan      html  css  js  c++  java
  • ES5新增数组的方法

    ES5新增数组的方法

    ES5新增数组常见方法(indexOf/forEach/map/filter/some/every)

    • .indexOf( data , start)
        检测数组中是否存在指定数据,存在返回索引,不存在返回-1,start表示从第几位开始查询。
    demo:
        var arr = ["a","45",67,true,"hello",67,45,25,13,89];
        
        console.log(arr.indexOf(67));           // 2
        console.log(arr.indexOf("world"));      // -1
        console.log(arr.indexOf("a"));          // 0
        console.log(arr.indexOf(67,3));         // 5
        console.log(arr.indexOf(67,6));         // -1
    • .forEach( function(val,idx,self){ } ); 循环,遍历数组
        数组的专属遍历方法,1个参数:回调函数,在回调函数身上又有三个参数
    var f = arr.forEach(function(val,idx,self){
        // console.log(val);
        // console.log(idx);
    })
    console.log(f);         // undefined
    • .map( function(val,idx,self){ } );
        不是专门用来遍历的,但是可以实现遍历,格式同forEach
        map的主要功能:可以用来获取数组中的数据,操作,并返回成新数组;原数组不变
    demo:
        "函数中的三个参数分别代表,该项的值,该项下标,数组本身"
        var m = arr.map(function(val,idx,self){
            console.log(val,idx,self)
            return val + "ly";
        })
        console.log(arr);       // Array(10) [ "a", "45", 67, true, "hello", 67, 45, 25, 13, 89 ]
        console.log(m);         // Array(10) [ "aly", "45ly", "67ly", "truely", "helloly", "67ly", "45ly", "25ly", "13ly", "89ly" ]
    • .filter( function(val,idx,self){ } )
        不是专门用来遍历的,但是可以实现遍历,格式同forEach
        主要功能:过滤,回调函数每次返回值为true时,会将这个遍历到的值放在新的数组中,在filter结束后,返回这个数组;如果为false,跳过;原数组保持不变
    demo:
      var
    f = arr.filter(function(val,idx,self){   console.log(val,idx,self)   return typeof val === "string";   })   console.log(f); // Array(3) [ "a", "45", "hello" ]
    • .some( function(val,idx,self){ } ) (课外补充)
        some意指“某些”,指是否“某些项”合乎条件。与下面的every算是好基友
        some存在一个值让callback返回true就可以了
    demo1:
        // 效果是我们判定这个数组是否有一个负数 使用.some
        var arr = [1,58,125,-12,458,12];
        var Boon = arr.some(function(e,i,arr){
          return e < 0;
        })
        console.log(Boon)      // ture
        
        var scores = [5, 8, 3, 10];
        var current = 7;
        
    demo2:
        function fn(score) {
            return score > current;
        }
        
        if (scores.some(fn)) {
            console.log("通过了!");        // "通过了!
        }
    • .every( function(val,idx,self){ } )
        所有函数的每个回调函数返回true的时候才会返回true,遇到一个false的时候终止执行,返回false
    demo1:
        // 效果是我们判定这个数组全是正数 使用.every
        var arr = [1,58,125,-12,458,12];
        var Boon = arr.every(function(e,i,arr){
          return e > 0;
        })
        console.log(Boon)      //false
    
    demo2:
        var scores = [5, 8, 3, 10];
        var current = 7;
        function fn(score) {
            return score > current;
        }
        if (scores.every(fn)) {
            console.log("通过了!");
        }else{
            console.log("没通过");          // "没通过"
        } 
  • 相关阅读:
    David Cutler NT之父
    VS2012 RC 编译Qt 4.8.2完整过程
    vm demo加固分析
    IDA dump so
    博客园首次发帖
    WebRTC本地选择codec(web本地模拟)
    Android 摄像头预览悬浮窗,可拖动,可显示在其他app上方
    [译] 清除浮动的新方法
    《学习HTML5游戏编程》译记
    Web中的Tip组件实现
  • 原文地址:https://www.cnblogs.com/wufenfen/p/11768589.html
Copyright © 2011-2022 走看看