zoukankan      html  css  js  c++  java
  • js中数组和字符串的方法总结

    一、数组方法简单总结为以下几种

      1、原有:     增、删、改、截、拼、复、排、转

      2、ES5扩展:   查、遍历

    增:

           前增
                var arr = [3,4,5,6,7];
                console.log(arr.unshift(1,2,[true,false]));        //8
                console.log(arr);                                //Array(8) [ 1, 2, (2) […], 3, 4, 5, 6, 7 ]

    后增 var arr = [3,4,5,6,7]; console.log(arr.push(1,2,[true,false])); //8 console.log(arr); //Array(8) [ 3, 4, 5, 6, 7, 1, 2, (2) […] ]

    删:

                前删
                var arr = [3,4,5,6,7];                            //3 删除的元素
                console.log(arr.shift());                        //Array(4) [ 4, 5, 6, 7 ]
                console.log(arr);
                后删
                var arr = [3,4,5,6,7];                            
                console.log(arr);
                console.log(arr.pop());                            //7
                console.log(arr);                                //Array(4) [ 3, 4, 5, 6 ] 

    改:

    var arr = [3,4,5,6,7];                            //
                console.log(arr);
                console.log(arr.splice(1,2)); //从下标1开始,删除2个元素    //Array [ 4, 5 ]
                console.log(arr);                                            //Array(3) [ 3, 6, 7 ]
                
                var arr = [3,4,5,6,7];
                console.log(arr);
                console.log(arr.splice(1,2,8,9,[true,false,null])); //Array [ 4, 5 ]        //把4,5改成8,9,[true,false,null]
                console.log(arr);                                    //Array(6) [ 3, 8, 9, (3) […], 6, 7 ]
                
                var arr = [3,4,5,6,7];
                console.log(arr);
                console.log(arr.splice(1,2,8,9,[true,false,null]));    //Array [ 4, 5 ]
                console.log(arr);                                    //Array(6) [ 3, 8, 9, (3) […], 6, 7 ]
                
                var arr = [3,4,5,6,7];
                console.log(arr);
                console.log(arr.splice(1,0,8,9,[true,false,null])); //Array []
                console.log(arr);                                    //Array(8) [ 3, 8, 9, (3) […], 4, 5, 6, 7 ]

    截:

  • 相关阅读:
    python threading 无法并行问题
    flask
    jinjia2 模板不解析html
    docker 配置lnmp环境(mac环境下)
    docker基础
    django初体验
    Centos6.5 编译安装Mysql 5.5.3.7
    Mysql InnoDB事务
    删除GitHub上项目中的某个文件
    转 WPF MVVM 循序渐进 (从基础到高级)
  • 原文地址:https://www.cnblogs.com/-roc/p/9972255.html
Copyright © 2011-2022 走看看