数组API(2)

     ES5中数组新增API:

      1. indexOf

            作用:检测当前数据是否在数组中存在;

            特点:如果存在返回相对应的下标,如果不存在则返回-1;

                     如果数组中存在多个需要判断的这个数据,则返回最近的这个数据的下标;

                    可以传递两个参数,第二个参数指定查询的起始位置;

            示例:

                 var arr = [10,20,30,40,50,30];
                 var index1= arr.indexOf(30);  
                 var index2 = arr.indexOf(80);
                 var index3 = arr.indexOf(30,3);
                console.log(index1);  //结果为2
                console.log(index2);  //结果为-1
                console.log(index3);  //结果为5

              
      2. lastIndexOf

            作用:检测当前数据是否在数组中存在;

            特点:如果存在返回相对应的下标,如果不存在则返回-1;

                     如果数组中存在多个需要判断的这个数据,则返回最后的这个数据的下标(也就是说它的查找方式是从右往左);

                    可以传递两个参数,第二个参数是指定查询的起始位置,然后从起始位置往左找;


            示例:

                 var arr = [10,20,30,40,50,30];
                 var index1= arr.lastIndexOf(30);  
                 var index2 = arr.indexOf(80);
                 var index3= arr.lastIndexOf(30,5);  
                 console.log(index1);  //结果为5
                 console.log(index2);  //结果为-1
                 console.log(index3);  //结果为5



     3. map

            作用:需要对数组中的数据做一些同一类型的操作时,可以用到map;

            特点:不会改变原数组,会有一个返回值, 返回值是一个新的数组;

                     map中除了有一个回调以外,还有第二个参数就是用来改变this指向的,此参数选用。
                            回调函数中的3个参数:
                                 item:数组中的数据
                                 index:数组中数据的下标
                                 array:当前遍历的数组

            示例:

                 var arr = [10,20,30,40,50];
    
                var newArr = arr.map(function(item,index,array){
                          console.log(item,index,array);//自己可以打印着看看
                          return item*=1.3;
                  })
    
                 console.log(arr);   //   [10,20,30,40,50]
                 console.log(newArr);  //  [ 13, 26, 39, 52, 65 ]

   4.   forEach

            作用:遍历;

            特点:没有返回值,不会改变原数组;

                     forEach中除了有一个回调以外,还有第二个参数 就是用来改变this指向的,此参数选用。

                            回调函数中的3个参数:
                                  item:数组中的数据
                                  index:数组中数据的下标
                                  array:当前遍历的数组

            示例:

                var arr = [10,20,30,40,50];
               var newArr  = []
               arr.forEach(function(item,index,array){
                     newArr.push(item*=1.3);
                     console.log(this);//当前this指向document,如果不改变this指向,则指向window
                  },document)

            console.log(arr);// [10,20,30,40,50]
            console.log(newArr);//[ 13, 26, 39, 52, 65 ]




    注:forEach和map是不能被终止的  也就是说return  和break根本不会终止它们的循环       

 
 5.  filter

            作用:通过某种条件对数据进行过滤筛选;

            特点:返回一个匹配过滤条件的新数组,不会改变原数组;

                     filter中除了有一个回调以外,还有第二个参数 就是用来改变this指向的,此参数选用。

                            回调函数中的3个参数:
                                 item:数组中的数据
                                 index:数组中数据的下标
                                 array:当前遍历的数组

            示例:
                       var arr = [10,20,30,40,50];

                     var newArr = arr.filter(function(item,index,array){
                         console.log(this);//当前this指向document,如果不改变this指向,则指向window
                       return item>30;
                    },document)
             
               
            console.log(arr);// [10,20,30,40,50]
            console.log(newArr);//[ 40,50 ]
   
  6.  reduce

            作用: 累积器;

            特点:返回一个匹配过滤条件的新数组,不会改变原数组;
                   
                  回调函数中的4个参数:
                                init:初始值,值为reduce函数的第二个参数
                                 current: 为数组中的当前值, 第一次为数组中下标为0的这个值 第二次为下标1的值  .....   
                                 index:数组中数据的下标
                                 array:当前遍历的数组

            示例:
                       var arr = [10,20,30,40,50];
                       var sum =  arr.reduce(function(init,current){
                         return current-init;
                        },0)
                      console.log(sum);//30 ;sum的计算过程: sum=50-(40-(30-(20-(10-0))));



  7.reduceRight
     作用同reduce,唯一的不同是,reduceRight 是从右至左遍历数组的元素。


  8.some
      作用: 测试数组中是否有某元素通过 callback 函数测试;


       特点:如果 callback 函数返回值为 true 则表示通过测试(有一个通过测试,则为真);

         回调函数中的3个参数:
                                v:数组中的数据
                                i:数组中数据的下标
                                arr:当前遍历的数组


            let arr=[1,3,6,4,2]
            let someRes = arr.some((v, i, arr) => {
                    return v > 5
                 })
           console.log(someRes) ;//  true

9. every
      作用: 测试数组中是否全部元素通过 callback 函数测试;


      特点:如果 callback 函数返回值为 true 则表示通过测试(全部通过测试,则为真);

               回调函数中的3个参数:
                                v:数组中的数据
                                i:数组中数据的下标
                                arr:当前遍历的数组


            let arr=[1,3,6,4,2]
            let everyRes = arr.every((v, i, arr) => {
                    return v > 5
                 })
           console.log(everyRes) ;// false

猜你喜欢

转载自www.cnblogs.com/class1/p/11002816.html