当前位置:网站首页>Arrays in JS

Arrays in JS

2022-06-23 15:11:00 Liujiayi_

Catalog

(1) Read the elements in the array

(2) Gets the length of the array

 (3) modify length


Array (Array)
- An array is also an object
- It functions like our normal objects , It's also used to store some values - The difference is that ordinary objects use strings as property names ,
Arrays use numbers as index elements
- Indexes :
from 0 The starting integer is the index
- The storage performance of arrays is better than that of ordinary objects , In development, we often use arrays to store some data


(1) Read the elements in the array

grammar : Array [ Indexes ]
If you read a nonexistent index , Instead of reporting an error, he returns undefined
 

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      var sz = new Array();
      sz[0] = 1;
      sz[1] = 2;
      sz[2] = 3;
      alert(sz[1]);
    </script>
  </body>
</html>

(2) Gets the length of the array


have access to length Property to get the length of the array ( Number of elements ) grammar . Array .length
For continuous arrays , Use length You can get the length of the array ( Number of elements )

For discontinuous arrays , Use length Will get the largest index of the array +1
Try not to create discontinuous arrays


 (3) modify length


If modified length Larger than the original length , Then the extra part will be empty

If modified length Less than the original length , The extra elements will be deleted

原网站

版权声明
本文为[Liujiayi_]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/174/202206231424014611.html