zoukankan      html  css  js  c++  java
  • js中数组Array的操作

    JS中数组有很多操作,作为前端多多少少知道点,但每次要用时都需要去百度,有点麻烦,所以在这儿做个备忘,简单总结些平时最常用的数组操作。

    shift删除原数组第一项,并返回删除元素的值

    var a = [1,2,3,4,5]; 
    var b = a.shift(); //a:[2,3,4,5]   b:1 
    注:如果数组为空则返回undefined 

    unshift将参数添加到原数组开头,并返回数组的长度 

    var a = [1,2,3,4,5]; 
    var b = a.unshift(-2,-1); //a:[-2,-1,1,2,3,4,5]   b:7 

    注:在IE6.0下测试返回值总为undefined,FF2.0下测试返回值为7,所以这个方法的返回值不可靠,需要用返回值时可用splice代替本方法来使用。

    pop删除原数组最后一项,并返回删除元素的值 

    var a = [1,2,3,4,5]; 
    var b = a.pop(); //a:[1,2,3,4]   b:5 //不用返回的话直接调用就可以了

    注:如果数组为空则返回undefined

    push将参数添加到原数组末尾,并返回数组的长度

    var a = [1,2,3,4,5]; 
    var b = a.push(6,7); //a:[1,2,3,4,5,6,7]   b:7 

    concat返回一个新数组,是将参数添加到原数组中构成的 

    var a = [1,2,3,4,5]; 
    var b = a.concat(6,7); //a:[1,2,3,4,5]   b:[1,2,3,4,5,6,7] 

    splice(start,deleteCount,val1,val2,...):从start位置开始删除deleteCount项,并从该位置起插入val1,val2,... 

    var a = [1,2,3,4,5]; 
    var b = a.splice(2,2,7,8,9); //a:[1,2,7,8,9,5]   b:[3,4] 
    var b = a.splice(0,1); //同shift 
    a.splice(0,0,-2,-1); var b = a.length; //同unshift 
    var b = a.splice(a.length-1,1); //同pop 
    a.splice(a.length,0,6,7); var b = a.length; //同push 

    reverse将数组反序 

    var a = [1,2,3,4,5]; 
    var b = a.reverse(); //a:[5,4,3,2,1]   b:[5,4,3,2,1] 

    sort(orderfunction):按指定的参数对数组进行排序 

    var a = [1,2,3,4,5]; 
    var b = a.sort(); //a:[1,2,3,4,5]   b:[1,2,3,4,5]

    sort()还可扩展

    join(separator):将数组的元素组起一个字符串,以separator为分隔符,省略的话则用默认用逗号为分隔符

    var a = [1,2,3,4,5]; 
    var b = a.join("|"); //a:[1,2,3,4,5]   b:"1|2|3|4|5"

    **数组的扩展

    //---------------------------------------------------  
    // 数组去重
    //---------------------------------------------------  
    Array.prototype.unique = function(){
            //思路:先排序,再根据排序后顺序,对比前后两个大小,若相等的话则取一个
            this.sort();
            var re=[this[0]];
            for(var i = 1; i < this.length; i++)
            {
                if( this[i] !== re[re.length-1])
                {
                    re.push(this[i]);
                }
            }
            return re;
    }
    
    //---------------------------------------------------  
    // 获得数组中最大的数
    //---------------------------------------------------  
    Array.prototype.max=function(){ 
        //方式一,判断大小的方式
    //    var i,
    //  max = this[0];
    //  for (i = 1; i < this.length; i++)
    //  {
    //      if (max < this[i])
    //      max = this[i];
    //  }
    //  return max;
        
        //方式二,Math方法
        return Math.max.apply({},this) 
        
        //方式三,sort方法,取最后一个数
    }
    
    //---------------------------------------------------  
    // 获得数组中最大的数
    //---------------------------------------------------  
    Array.prototype.min=function(){ 
        //方式一,判断大小的方式
    //    var i,
    //  min = this[0];
    //  for (i = 1; i < this.length; i++)
    //  {
    //      if (min > this[i])
    //      min = this[i];
    //  }
    //  return min;
        
        //方式二,Math方法
        return Math.min.apply({},this) ;
        
        //方式三,sort方法,取第一个数
    }

    部分参考博友相关博文,无商业用途,仅用于知识标记。

    尊重原作者,贴原博文地址:http://www.cnblogs.com/yuzhongwusan/archive/2008/12/15/1355378.html 

  • 相关阅读:
    【转】VS2010中 C++创建DLL图解
    [转]error: 'retainCount' is unavailable: not available in automatic reference counting mode
    [转]关于NSAutoreleasePool' is unavailable: not available in automatic reference counting mode的解决方法
    【转】 Tomcat v7.0 Server at localhost was unable to start within 45
    【转】Server Tomcat v7.0 Server at localhost was unable to start within 45 seconds. If
    【转】SVN管理多个项目版本库
    【转】eclipse安装SVN插件的两种方法
    【转】MYSQL启用日志,和查看日志
    【转】Repository has not been enabled to accept revision propchanges
    【转】SVN库的迁移
  • 原文地址:https://www.cnblogs.com/dingdong/p/5340565.html
Copyright © 2011-2022 走看看