数组方法数组函数排序

一丶数组的操作方法
1.reverse() :对原数组进行翻转
var a1=[23,12,56,34]
        a1.reverse()
        console.log(a1);  //[ 34, 56, 12, 23 ]
2.join指定分割符,将数组转化为字符串
 // var a1="he,ll,oo,mm"
        字符串转数组
        // var a2=a1.split(",")  //[he,ll,oo,mm]
        数组转字符串
        // a2.join("")  //helloomm
        // a2.join("3")  //he3ll3oo3mm
3.concat  连接数组,将数据添加到数组的末尾
        不修改原数组生成新的数组
        只能打散一维数组,不会打散数组中包含的数组
 // a1=[1,2,3]
        // a2=[4,5,6]
        // console.log(a1.concat(a2));  //[ 1, 2, 3, 4, 5, 6 ]
        // console.log(a1.concat(55,66));   //[ 1, 2, 3, 55, 66 ]
        //console.log(a1.concat([33,88],[44,77])); 
        //[ 1, 2, 3, 33, 88, 44, 77 ]
        // console.log(a1.concat([[11,22]],[33,44]),"abc"); 
        //[ 1, 2, 3, [ 11, 22 ], 33, 44 ]
4.slice 截取子数组:
(1)生成新的数组,不修改原数组
(2)a1.slice(2,4) 起始包含,终止不包含
(3)a1.slice(2)   起始到数组末尾
var a1=[12,34,56,67,89]
        0  1  2   3  4
        -5 -4 -3  -2  -1
         a1=[1,2,3,4,5,6,7]
    console.log(a1)                 // [12,34,45,67,89]
    console.log(a1.slice(2,4))      // [45,67]
    console.log(a1.slice(2))        // [45,67,89]
    console.log(a1.slice(2,10))     // [45,67,89]
    console.log(a1.slice(-4,-2))    // [34,45]

5.splice(arg1,arg2,arg3.....) 增加和删除数组元素
arg1---起始位置
arg2---被删除的元素个数
arg3及后面所有参数---要插入的元素
如只有一个参数,从起始位置删除后面所有元素
修改原数组,返回被删除元素
如第一个参数为负数,则从右到左查找
如第二个参数为负数,按0处理
a1=[1,2,3,4,5,6,7]
    a1.splice(1,2,3,4,5)  
    a1.splice(2)            如只有一个参数,从起始位置删除后面所有的元素
    a1.splice(2,2)        
    a1.splice(2,0)        
    a1.splice(2,0,0,0,0)  
    a1.splice(6,2,3,4)    
    a1.splice(-2,-3,6,7,8)  如果第一个参数为负数,则从右到左查找
                            如果第二个参数为负数,按0处理

6.数组删除
    (1)splice
    (2)delete: 仅删除元素本身,不能删除元素的空间
        var a1 = [3,4,5]
        del a1[0]
二.字符串拼接操作效率
   var s1="hello"
      var sums1=""
      var arr1=[]
      var begintime=new Date().getMilliseconds()
//      for(var i=0;i<1000000;i++){
//          sums1=sums1+s1
//      }
//      for(var i=0;i<1000000;i++){
//            sums1.concat(s1)
//        }
        for(i=0;i<1000000;i++){
            arr1.push(s1)
        }
        arr1.join("")

      var endtime=new Date().getMilliseconds()
      console.log(endtime-begintime)


三.数组检测
    var  a1 = [2,3,4]
    typeof(a1)                      object
    1.Array.isArray(a1)             true
    2.if(a1 instanceof Array){}ray         true
    3.a1.constructor      true

四.数组的     true
    3.a1.constructor==ArtoString()
    所有对象都有toString()  valueof()   toLocalstring()
    toString():返回数组中每个值的字符串形式,以逗号分隔进行拼接
               数组转字符串
    
    var  a1 = [3,4,5]
    console.log(a1.toString())          //3,4,5

扩展: 
p1={"name":"zs"}
console.log(p1.toString())

五丶数组下标
var a1=[4,5,6,7]
1.a1[0]  0--下标
2.范围  大于0, 小于2^32-1 整数
3.如下标是负数,浮点数,布尔型等,js会自动将其转换为字符串
  var a1[4,5,6,7]
  a1[true]   //undefinded

  a1[-2]    //undefinded
  a1[2,3]    //undefinded
4.下标是可以不断递增的表达式
  var a1=[]
  for(var i=0;i<10;i++){
    a1[i++]=i
    a1[0]=1
    a1[2]=3
       -
       -
       -
       -
  }
  console.log(a1)

  六丶排序
    数组.sort(比较函数)
    比较函数---具有两个参数 f1(a,b)
           函数值判断返回值
           返回值大于0    a排在b的后面
           返回值小于0    a排在b的前面
           返回值等于0    a,b不动
    var a1=[23,4,5,12,78,16]
        console.log(a1.sort());
        //[ 12, 16, 23, 4, 5, 78 ] Ascii码 

    var a1=[23,4,5,12,78,16]
        function f1(a,b) {
            //return a-b 
            if(a-b>0)
              return 1   a在b后
            else(a-b<0)
              return -1  b在a前
            else
              return 0   a和b不动
        }
        console.log(a1.sort(f1));  
        //[ 4, 5, 12, 16, 23, 78 ]

  

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值