A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Array/length below:

Array:length - JavaScript | MDN

Array:length

Baseline Widely available

length 是 Array 的实例属性,表示该数组中元素的个数。该值是一个无符号 32 位整数,并且其数值总是大于数组最大索引。

尝试一下
const clothing = ["shoes", "shirts", "socks", "sweaters"];

console.log(clothing.length);
// Expected output: 4
值

一个小于 232 的非负整数。

可写 是 可枚举 否 可配置 否 描述

length 属性的值是一个小于 232 的非负整数

const listA = [1, 2, 3];
const listB = new Array(6);

console.log(listA.length);
// 3

console.log(listB.length);
// 6

listB.length = 2 ** 32; // 4294967296
// RangeError: Invalid array length

const listC = new Array(-100); // 负数是不允许的
// RangeError: Invalid array length

数组对象会观察 length 属性,并自动将 length 值与数组的内容同步。这意味着:

当 length 被设置为比当前长度更大的值时,数组通过添加空槽来扩展,而不是实际的 undefined 值。空槽与数组方法有一些特殊的交互作用;详见数组方法和空槽。

const arr = [1, 2];
console.log(arr);
// [ 1, 2 ]

arr.length = 5; // 将数组长度设置为 5,而当前为 2。
console.log(arr);
// [ 1, 2, <3 empty items> ]

arr.forEach((element) => console.log(element));
// 1
// 2

另请参阅:length 与数值属性的关系。

示例 遍历数组

在下面的示例中,通过查看 length 属性来遍历数组 numbers。然后将每个元素中的值加倍。

const numbers = [1, 2, 3, 4, 5];
const length = numbers.length;
for (let i = 0; i < length; i++) {
  numbers[i] *= 2;
}
// 遍历后的结果 [2, 4, 6, 8, 10]
截断数组

在下面的示例中,如果当前长度大于 3,数组 numbers 将会缩短为 3。

const numbers = [1, 2, 3, 4, 5];

if (numbers.length > 3) {
  numbers.length = 3;
}

console.log(numbers); // [1, 2, 3]
console.log(numbers.length); // 3
console.log(numbers[3]); // undefined;多余的元素会被删除
创建固定长度的空数组

将 length 设置为大于当前长度的值将会创建一个稀疏数组。

const numbers = [];
numbers.length = 3;
console.log(numbers); // [empty x 3]
长度不可写的数组

当添加的元素超过当前长度时,数组会自动更新 length 属性。如果 length 属性设置为不可写,则数组将无法更新它。在严格模式中这会导致错误。

"use strict";

const numbers = [1, 2, 3, 4, 5];
Object.defineProperty(numbers, "length", { writable: false });
numbers[5] = 6; // TypeError: Cannot assign to read only property 'length' of object '[object Array]'
numbers.push(5); // // TypeError: Cannot assign to read only property 'length' of object '[object Array]'
规范 浏览器兼容性 参见

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4