数组拆分和合并

作者: 小雪洁 | 来源:发表于2020-03-16 14:05 被阅读0次
    <!DOCTYPE html>
    <html>
        <head>
            <meta charset="utf-8">
            <title>拆分和合并</title>
        </head>
        <body>
            <script>
                //字符串转成数组
                let str="hxj,ydc";
                let arr=str.split(",");
                console.log(arr);// ["hxj", "ydc"]
                str=arr.join(",");
                console.log(str);//hxj,ydc
                //数组连接
                let a=[1,2,3,4];
                let b=[4,5,6];
                let c=[6,7,8];
                //a=a.concat(b,c);
                //console.log(a);//[1, 2, 3, 4, 4, 5, 6, 6, 7, 8]
                //使用点语法连接数组
                a=[...a,...b,...c];
                console.log(a);//[1, 2, 3, 4, 4, 5, 6, 6, 7, 8]
                //数组元素内部的复制
                //arr.copyWithin(target,start,end),
                //target复制到目标索引,复制内容的索引[start,end);
                let fruits = ["Banana", "Orange", "Apple", "Mango", "Kiwi", "Papaya"];
                fruits.copyWithin(2, 0, 2);
                console.log(fruits);// ["Banana", "Orange", "Banana", "Orange", "Kiwi", "Papaya"]
                
            </script>
        </body>
    </html>
    
    

    相关文章

      网友评论

        本文标题:数组拆分和合并

        本文链接:https://www.haomeiwen.com/subject/nzsgehtx.html