Javascript array assignment and value collection

Javascript array assignment and value collection

Table of contents

Javascript array assignment and value collection

1. Assignment of array elements

1. Directly assign value when creating Array object

2. Use the element subscripts of the Array object to assign values ​​to the array.

2. Obtaining array elements


 

1. Assignment of array elements

There are two ways to assign values ​​to array elements:

  • (1) Directly assign value when creating Array object;
  • (2) Use the element subscripts of the Array object to assign values ​​to the array;

In fact, we have already touched on these two methods in the previous two sections. Here I will give you a systematic summary:

 

1. Directly assign value when creating Array object

grammar:

var 数组名 = new Array(元素1,元素2,…,元素n);

Example:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <script type="text/javascript">
        //创建数组的同时对元素赋值
        var arr=new Array("html","css","javascript");
        document.write(arr.length);
    </script>
</head>
<body>
</body>
</html>

The preview effect in the browser is as follows:

 

a98ebca885f974d844d1a8c71ff1a23a.png

 

2. Use the element subscripts of the Array object to assign values ​​to the array.

This method can input element values ​​intoArray object at any time, or modify the value of any element in the array.

grammar:

var 数组名 = new Array();
数组名[0] = 元素1;
数组名[1] = 元素2;
……
数组名[n] = 元素(n-1);

Example:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <script type="text/javascript">
        //创建数组
        var arr=new Array();
        //通过下标对数组元素赋值
        arr[0] = "html";
        arr[1] = "css";
        arr[2] = "javascript";
        //修改arr[0](即第1个元素)的值
        arr[0] = "vvv";
        document.write(arr[0]);
    </script>
</head>
<body>
</body>
</html>

 

 

 

2. Obtaining array elements

In JavaScript, the value of an item in an array is obtained through the subscript of the array element.

Example:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <script type="text/javascript">
        //创建数组
        var arr=new Array("中国","广东","广州","天河","暨大");
        document.write(arr[4]);
    </script>
</head>
<body>
</body>
</html>

The preview effect in the browser is as follows:

 

e4d0fdf6c122dd0416d1c1a2a55ea054.png

 

Example 2:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <script type="text/javascript">
        //创建数组
        var arr=new Array("中国","广东","广州","天河","暨大");
        //利用for循环获取所有数组元素
        for(var i=0;i<arr.length;i++)
        {
            document.write(arr[i] + "<br/>");
        }
    </script>
</head>
<body>
</body>
</html>

The preview effect in the browser is as follows:

 

7d022f6f0b53e48933ec03978c1cadba.png

Where arr.length means getting the length of the array arr.

 

Guess you like

Origin blog.csdn.net/2301_78835635/article/details/134738494