数组常用的方法

2018-06-28  本文已影响17人  箫筱乐

数组的常用方法

   let arr=[]
   arr.push(1,2,3)
   console.log(arr)   //输出结果为[1,2,3]

   arr.pop()
   console.log(arr)  //输出结果为[1,2] 

   let arr1=[1,2,3]
   arr1.unshift(4,5)
   console.log(arr1) //输出结果为 [4,5,1,2,3]
    
   arr1.shift()
   console.log(arr1)  //输出结果为[5,1,2,3]
   

push:向末尾增加一项,返回数值是数组的新长度
pop:删除数组的末尾向,返回值是删除数组的数组项
unshift:想数组的开头增加一项,返回数值是数组的新长度
shift:删除数组的开头第一项,返回值是删除数组的数组项
以上四种方法都改变原数组。

slice

作用:字符串截取(复制数组)
参数:arr.slice(start,end) start算本身,end最后一位不算
是否改变原数组:不改变

   let arr=[1,2,3,4,5]
   arr.slice(0,2)   //输出结果为[1,2]                

splice

作用:增、删、改
参数:arr.splice(index,homany,item...item10) index算本身,homay算本身
是否改变原数组:改变

   let arr1=[1,2,3,4]
   //1.作用:添加
   arr1.splice(1,0,123,234)  //输出结果为[1,123,234,2,3,4] 
   //2.作用:删除
   arr1.splice(1,2)          //输出结果为[1,4,5]
   // 3.作用:添加&&删除
   arr1.splice(1,2,123,234)       //输出结果为[1,123,234,4]    

join

作用: 用指定的分隔符将数组的每一项拼接成字符串
参数:arr.join('-')
是否改变原数组:不改变

     let arr=[1,2,3,4]
     let newArr=arr.join('-')
     console.log(newArr)   //输出结果为‘1-2-3-4’

concat

作用:用于连接两个或多个数组
参数:参数可以是具体的值,也可以是数组对象,可以是任意多个
是否改变原数组:不改变

    let arr=[1,2,3,4]
    arr.join('-')
    console.log(arr)   //输出结果为‘1-2-3-4’   typeof arr 为字符串不是数组了

sort

作用:对数组元素进行排序
参数:arr.sort(function(a,b){return a-b})
是否改变原数组:改变

    let arr=[1,9,5,7,3,5,2,9,8]
    arr.sort(function(a,b){
         return a-b  //从小到大   b-a从大到小
   })
   console.log(arr)  //输出结果为[1,2,3,5,5,7,8,9,9]  

reverse

作用:倒叙数组
参数:无
是否改变原数组:改变

   let arr=[1,4,2,5,1,9]
   arr.reverse()
   console.log(arr)   //输出结果为[9,1,5,2,4,1]     倒叙

indeOf

作用:查找指定元素的位置
参数:arr.indexOf(item,start) //item检查的值,start在哪里检查一般不写
返回值:返回第一次找到的索引,未找到返回-1
是否改变原数组:不改变

   let arr=[1,23,4325345,1231,]
   console.log(arr.indexOf(1231))   //输出结果为3

lastIndexOf

作用:查找指定元素最后出现的位置
参数:arr.indexOf(item,start) //item检查的值,start在哪里检查一般不写
返回值:返回第一次找到的索引,未找到返回-1
是否改变原数组:不改变

    let arr=[1,1,1,1,1]
    arr.lastIndexOf(1)  //输出结果为5

forEach

作用:循环遍历数组每一项
参数:函数 ary.forEach(function(item,index,ary){}) item:每一项 index:索引 ary:当前数组
是否改变原数组:不改变

    let arr=[1,1,2,3,4,5]
    arr.forEach(function(item,index,arr){
          console.log(item,index,arr)
          //1   0     [1,1,2,3,45]
          //1   2     [1,1,2,3,45]   .....以此类推
   })

map

作用:数组中的元素为原始元素调用函数处理的值
参数:ary.mp.map(function(item,index.ary){})
是否改变原数组:不改变

    let arr=['a','b','c','d']
    let res=arr.map(function(item,index,ary){
                     item+1
              })
     console.log(res)  //输出结果为 ['a1','b1','c1','d1']

filter

作用:创建一个新的数组,新数组中的元素是通过检查指定指定数组中符合条件的所有元素
参数:ary.filter(function(item,index,ary){})
是否改变原数组:不改变

    let arr=[1,23,4,1,5,6,12,5,11]
    //我们要把这个数组里面大于10的找出来
    let res=arr.filter(function(item){
                    return item>10
                 })
   console.log(res)   //输出结果为[23,12,11]

every

作用:检查数组所有元素是否都符合指定调价
参数:ary.every(function(item,index,ary){}) item:每一项 index:索引 ary:当前数组
是否改变原数组:不改变

   let arr=[1,2,3,4,5,6]
   let res=arr.evry(function(item){
                     return item<4
                 })
   let res1=arr.evry(function(item){
                     return item<6
                 })
console.log(res)    //输出结果为false
console.log(res1)  //输出结果为true

some

作用:检测数组中的元素是否满足指定条件
参数:函数 ary.some(function(item,index,ary){}) item:每一项 index:索引 ary:当前数组
是否改变原数组:不改变

   var ary = [1,2,3,4,5,6]
   var res = ary.some(function(item){
                     return item<3;
                 })
   console.log(res)  // 输出结果为true;

Array.from()

作用:把伪数组转成真正的数组
参数:arr.from(伪数组)

    let json={
              '0': 'a',
             '1': 'b',
             '2': 'c',
            length:3
    } 
    //在Es5中我们是这样转:
    var arr1=[].slice.call(json)   
    console.log(arr1)  //输出结果为['a','b','c']
    //在Es6中我们新增了from命令
    let arr2=Array.from(json)
    console.log(arr2)     //输出结果为['a','b','c']

Array.of()

作用:将一组值,转换为数组
参数:Array.of(1,23,3)

   let arr=Array.of(1,2,3)
   console.log(arr)   //输出结果为[1,2,3]  
   //Array.of() 可以用下main代码来模拟实线:
   function ArrayOf(){
               return [].slice.call(arguments);
    }

copyWithin

作用:在当前的数组,将指定的元素赋值到指定的位置(会覆盖其他数值)
参数:arr.copyWithin(target,start,end)
target从位置开始替换数据
start在该位置读取数据
end在该位置停止读取数据
是否改变原数组:改变

     let arr=[1,23,4,56,3]
     arr.coppyWithin(0,2)  
     console.log(arr)  //输出结果为[4,56,3,56,3]

find

作用:用于找出第一个符合条件的元素
参数:arr.find((item,index,arr)=>) item当前的值,index当前的索引值,arr当前的数组

   let arr=[11,23,5,123]
   let res=arr.find(function(item,index){
                return item<10
            })
  console.log(res)   //5

findIndex

作用:返回第一个符合条件元素的位置
参数:arr.find((item,index,arr)=>) item当前的值,index当前的索引值,arr当前的数组

   let arr=[11,23,5,123]
   let res=arr.findIndex(function(item,index){
                return item<10
            })
    console.log(res)   //2

fill

作用:填充数组
参数:arr.fill('con','start','ed')
是否改变原数组:改变

   let arr=[1,23,45]
   arr.fill(2)
   console.log(arr)    //[2,2,2]
   arr.fill(2,1,2)
   console.log(arr)   // [1,2,45]

includes

作用:数组是否包含给定的值返回boolean值
参数:arr.includes(target,start) target要找的值 start在这个位置找如果为负数那就是在最后往前找

    let arr=[1,2,34,56]
    let res=arr.includes(2)
   console.log(res)    //true 
   //他与indexOf的区别
   //indexOf 内部使用严格等运算符(====)那么就会导致NaN的误判
    [NaN].indexOf(NaN)     //-1
    [NaN].includes(NaN)    //true
上一篇 下一篇

猜你喜欢

热点阅读