Array.prototype.at()

Baseline 2022

Newly available

Since March 2022, this feature works across the latest devices and browser versions. This feature might not work in older devices or browsers.

Array 实例的 at() 方法采用整数值并返回该索引处的项目,允许正整数和负整数。负整数从数组中的最后一项开始倒数。

¥The at() method of Array instances takes an integer value and returns the item at that index, allowing for positive and negative integers. Negative integers count back from the last item in the array.

Try it

语法

¥Syntax

js
at(index)

参数

¥Parameters

index

要返回的数组元素的从零开始的索引,转换为整数。负索引从数组末尾开始倒数 — 如果访问了 index < 0index + array.length

返回值

¥Return value

数组中与给定索引匹配的元素。如果 index < -array.lengthindex >= array.length 则始终返回 undefined,而不尝试访问相应的属性。

¥The element in the array matching the given index. Always returns undefined if index < -array.length or index >= array.length without attempting to access the corresponding property.

描述

¥Description

at() 方法相当于 index 为非负时的括号表示法。例如,array[0]array.at(0) 都返回第一项。但是,当从数组末尾开始计数元素时,你不能像在 Python 或 R 中那样使用 array[-1],因为方括号内的所有值都被字面上视为字符串属性,因此你最终将读取 array["-1"],这只是一个 普通字符串属性而不是数组索引。

¥The at() method is equivalent to the bracket notation when index is non-negative. For example, array[0] and array.at(0) both return the first item. However, when counting elements from the end of the array, you cannot use array[-1] like you may in Python or R, because all values inside the square brackets are treated literally as string properties, so you will end up reading array["-1"], which is just a normal string property instead of an array index.

通常的做法是访问 length 并从中计算索引 - 例如,array[array.length - 1]at() 方法允许相对索引,因此可以缩短为 array.at(-1)

¥The usual practice is to access length and calculate the index from that — for example, array[array.length - 1]. The at() method allows relative indexing, so this can be shortened to array.at(-1).

通过将 at()with() 组合,你可以使用负索引(分别)读取和写入数组。

¥By combining at() with with(), you can both read and write (respectively) an array using negative indices.

at() 方法是 generic。它只期望 this 值具有 length 属性和整数键控属性。

¥The at() method is generic. It only expects the this value to have a length property and integer-keyed properties.

示例

¥Examples

返回数组的最后一个值

¥Return the last value of an array

以下示例提供了一个函数,该函数返回在指定数组中找到的最后一个元素。

¥The following example provides a function which returns the last element found in a specified array.

js
// Our array with items
const cart = ["apple", "banana", "pear"];

// A function which returns the last item of a given array
function returnLast(arr) {
  return arr.at(-1);
}

// Get the last item of our array 'cart'
const item1 = returnLast(cart);
console.log(item1); // 'pear'

// Add an item to our 'cart' array
cart.push("orange");
const item2 = returnLast(cart);
console.log(item2); // 'orange'

比较方法

¥Comparing methods

此示例比较了选择 Array 的倒数第二个(最后一个)项目的不同方法。虽然下面显示的所有方法都是有效的,但此示例突出了 at() 方法的简洁性和可读性。

¥This example compares different ways to select the penultimate (last but one) item of an Array. While all the methods shown below are valid, this example highlights the succinctness and readability of the at() method.

js
// Our array with items
const colors = ["red", "green", "blue"];

// Using length property
const lengthWay = colors[colors.length - 2];
console.log(lengthWay); // 'green'

// Using slice() method. Note an array is returned
const sliceWay = colors.slice(-2, -1);
console.log(sliceWay[0]); // 'green'

// Using at() method
const atWay = colors.at(-2);
console.log(atWay); // 'green'

对非数组对象调用 at()

¥Calling at() on non-array objects

at() 方法读取 thislength 属性并计算要访问的索引。

¥The at() method reads the length property of this and calculates the index to access.

js
const arrayLike = {
  length: 2,
  0: "a",
  1: "b",
  2: "c", // ignored by at() since length is 2
};
console.log(Array.prototype.at.call(arrayLike, 0)); // "a"
console.log(Array.prototype.at.call(arrayLike, 2)); // undefined

规范

Specification
ECMAScript Language Specification
# sec-array.prototype.at

¥Specifications

浏览器兼容性

BCD tables only load in the browser

¥Browser compatibility

也可以看看