Home  >  Article  >  Web Front-end  >  How to determine how many items there are in an array in es6

How to determine how many items there are in an array in es6

青灯夜游
青灯夜游Original
2023-01-18 19:22:462012browse

In es6, you can use the length attribute of the array object to determine how many items there are in the array, that is, to get the number of elements in the array; this attribute can return the number of elements in the array, just use "array .length" statement can return a value representing the number of elements of the array object, that is, the length value.

How to determine how many items there are in an array in es6

The operating environment of this tutorial: Windows 7 system, ECMAScript version 6, Dell G3 computer.

es6 To determine how many items there are in the array is to determine the number of elements in the array, that is, to find the length of the array. So how to ask for it?

In es6, you can use the length property of the array object to get the length of the array.

Each array has a length attribute, which returns the maximum length of the array, that is, its value is equal to the maximum subscript value plus 1. Since the numeric subscript must be less than 2^32-1, the maximum value of the length attribute is equal to 2^32-1.

Example 1:

var arr=[2,6,1,5,22,3,66,12,9];
arr.length;

How to determine how many items there are in an array in es6

Example 2

The following code defines a Empty array, and then assign a value to the element whose index is equal to 100, the length property returns 101. Therefore, the length attribute cannot reflect the actual number of array elements.

var a = [];  //声明空数组
a[100] = 2;
console.log(a.length);  //返回101

Output:

How to determine how many items there are in an array in es6

length property is readable and writable and is a dynamic property. The length attribute value is also automatically updated as the array elements change. At the same time, if the length attribute value is reset, it will also affect the elements of the array. The specific instructions are as follows:

  • If the length attribute is set to a value smaller than the current length value, the array will Truncated, element values ​​beyond the new length will be lost.

  • If the length attribute is set to a value greater than the current length value, then the empty array will be added to the end of the array, making the array grow to the newly specified length, and read the value All are undefined.

Example 3

The following code demonstrates the impact of dynamic changes in the length attribute value on the array.

var a = [1,2,3];  //声明数组直接量
a.length = 5;  //增长数组长度
console.log(a[4]);  //返回undefined,说明该元素还没有被赋值
a.length = 2;  //缩短数组长度
console.log(a[2]);  //返回undefined,说明该元素的值已经丢失

Output:

How to determine how many items there are in an array in es6

[Related recommendations: javascript learning tutorial]

The above is the detailed content of How to determine how many items there are in an array in es6. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:Does webpack support es6?Next article:Does webpack support es6?