• JS 数组方法 实例


       /* 数组转为字符串,用join(" ")方法,原数组不变,返回新字符串 */
            var arr = new Array;
            arr[0] = "hello";
            arr[1] = "melin";
            var str =arr.join(" ");
            console.log(arr);       //[“hello”,"melin"]
            console.log(arr.length);//2
            console.log(str);       //hello melin
            console.log(str.length);//11
    
        /* 字符转为数组,用split(" ")方法,原字符串不变,返回新数组 */
            var str = "hello melin go!";
            var arr =[];
            arr = str.split(" ");
            console.log(str);       //hello melin go!
            console.log(str.length);//15
            console.log(arr);       //["hello","melin","go!"]
            console.log(arr.length);//3
            console.log(arr[0]);    //hello
            console.log(arr[1]);    //melin
            console.log(arr[2]);    //go!
    
        /* 数组,shift方法 ,对原数组删除其第一个元素,返回删除的元素*/
            var arr1 = [5,6,7,8];
            var p;
            p = arr1.shift();
            console.log(arr1); //[6,7,8]
            console.log(p);    //5
    
        /* 数组,pop方法 ,对原数组删除其最后一个元素,返回删除的元素*/        
            var arr1 = [5,6,7,8];
            var p ;
            p = arr1.pop();
            console.log(arr1);  //[5,6,7]
            console.log(p);     //8
            
        /* 数组,push方法 ,对原数组追加最后一个元素,返回数组长度 */ 
            var arr1 = [5,6,7,8];
            var c = 9,p;
            p = arr1.push(c);
            console.log(arr1); //[5,6,7,8,9]
            console.log(p);    //5
    
        /* 数组,unshift方法 ,对原数组增加第一个元素,返回数组长度*/ 
            var arr1 = [5,6,7,8];
            var c = 4,p;
            p = arr1.unshift(c);
            console.log(arr1); //[4,5,6,7,8]
            console.log(p);    //5
    
        /* 数组,splice(start,num,arr1,arr2...)方法 ,对原数组删除从start开始num个原数组
        ,返回删除的数组 */ 
            var arr1 = [5,6,7,8];
            var arr2;
            var arr3 = [9,10];
            arr2 = arr1.splice(1,2,arr3,4);
            console.log(arr1);    //[5,[9,10],4,8]
            console.log(arr2);    //[6,7]   
    
        /* 数组,slice(start,end)方法 ,对原数组删除从start开始到end结束
        ,返回删除的数组 */ 
            var arr1 = [4,5,6,7];
            var arr2,arr3,arr4,p;
            arr2 = arr1.slice(2,3);
            arr3 = arr1.slice(2);
            arr4 = arr1.slice(-3,-2);
            console.log(arr1); //[4,5,6,7]
            console.log(arr2); //[6]
            console.log(arr3); //[6,7]
            console.log(arr4); //[5]
    
        /* 数组,concat方法 ,连接两数组,返回新数组*/ 
            var arr1 = [4,5,6,7],arr2 = [8,9];
            var arr3,arr4;
            arr3 = arr1.concat(arr2);
            arr4 = arr2.concat(arr1);
            console.log(arr1); //[4,5,6,7]
            console.log(arr2); //[8,9]
            console.log(arr3); //[4,5,6,7,8,9]
            console.log(arr4); //[8,9,4,5,6,7]
    
        /* 数组,reverse方法 ,反转原数组,返回新数组*/ 
            var arr1 = [4,5,6,7];
            var arr2;
            arr2 = arr1.reverse();
            console.log(arr1); //[7,6,5,4]
            console.log(arr2); //[7,6,5,4]
  • 相关阅读:
    显示和隐藏密码
    如何给input的右上角加个清除的按钮?
    手机号中间四位用*号代替
    利用JS+正则表达式获取URL的GET数据
    腾讯QQ头像/QQ网名等相关获取API接口
    jqurey 在编辑的时候为select设置选中项
    html面试题
    js splice和delete删除数组长度会变化吗
    webp与jpg、png比较,它有什么优劣势?如何选择?
    iOS开发技术之应用代码注入防护
  • 原文地址:https://www.cnblogs.com/xmyun/p/5923759.html
Copyright © 2020-2023  润新知