Properties and methods of Array objects in JavaScript

Array object: an array variable is an array object

length property: dynamically obtains the length of the array. For example: var len = arrObj.length


##join()

  • Function: Convert an array into a string. Returns a string.

  • Syntax: arrObj.join (connection number)

  • Description: Connect an array into one character using the specified "connection number" string.

  • <!DOCTYPE HTML>
    <html>
        <head>
            <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
            <title>php.cn</title>
            <script>
               //将以下字符串转成数组, 然后再转成字符串
               var str = "北京,上海,深圳,南京,合肥";  
               //转成数组
               var arr=str.split(",");
               //再转换成字符串
               str = arr.join(",") ;
               document.write("类型是:"+typeof(str)+",字符串为:"+str);
            </script>
        </head>
        <body>
        </body>
    </html>


##reverse()

    Function: Reverse the order of elements in the array.
  • Syntax: arrObj.reverse()
  • Parameters: None
  • <!DOCTYPE HTML>
    <html>
        <head>
            <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
            <title>php.cn</title>
            <script>
              var arr=[1,2,3,4,5,6,7,8];
              arr.reverse();
              document.write(arr);
            </script>
        </head>
        <body>
        </body>
    </html>


Deletion and addition of array elements

  • delete operation symbol, only the value of the array element can be deleted, but the space occupied is still there, and the total length has not changed (arr.length).

  • #Previous array elements can only be added backward, not forward.

    <!DOCTYPE HTML>
    <html>
        <head>
            <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
            <title>php.cn</title>
            <script>
                //使用delete删除元素,查看长度是否改变
                var arr=[1,2,3,4,5,6,7,8];
                document.write("数组的长度为"+arr.length+",值为"+arr+"<br/>");
                delete arr[0];
                delete arr[1];
                delete arr[2];
                document.write("数组的长度为"+arr.length+",值为"+arr+"<br/>");
            </script>
        </head>
        <body>
        </body>
    </html>
  • shift(): Delete the first element in the array, return the deleted value, and reduce the length by 1.

  • pop(): Delete the last element in the array, return the deleted value, and reduce the length by 1.

    <!DOCTYPE HTML>
    <html>
        <head>
            <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
            <title>php.cn</title>
            <script>
                //使用delete删除元素,查看长度是否改变
                var arr=[1,2,3,4,5,6,7,8];
                document.write("数组的长度为"+arr.length+",值为"+arr+"<br/>");
                arr.shift();
                document.write("数组的长度为"+arr.length+",值为"+arr+"<br/>");
                arr.pop();
                document.write("数组的长度为"+arr.length+",值为"+arr+"<br/>");
            </script>
        </head>
        <body>
        </body>
    </html>
  • unshift(): Add one or more array elements to the front of the array, and the length needs to be changed. arrObj.unshift("a", "b", "c")

  • push(): Add one or more array elements to the end of the array, length To change. arrObj.push(“a” , “b” , “c”)

    <!DOCTYPE HTML>
    <html>
        <head>
            <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
            <title>php.cn</title>
            <script>
                //使用delete删除元素,查看长度是否改变
                var arr=[1,2,3,4,5,6,7,8];
                document.write("数组的长度为"+arr.length+",值为"+arr+"<br/>");
                arr.unshift(0);
                document.write("数组的长度为"+arr.length+",值为"+arr+"<br/>");
                arr.push(9);
                document.write("数组的长度为"+arr.length+",值为"+arr+"<br/>");
            </script>
        </head>
        <body>
        </body>
    </html>
Continuing Learning
||
<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>php.cn</title> <script> //将以下字符串转成数组, 然后再转成字符串 var str = "北京,上海,深圳,南京,合肥"; //转成数组 var arr=str.split(","); //再转换成字符串 str = arr.join(",") ; document.write("类型是:"+typeof(str)+",字符串为:"+str); </script> </head> <body> </body> </html>
submitReset Code