Array.prototype.shift()

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.

Array 实例的 shift() 方法从数组中删除第一个元素并返回该删除的元素。该方法改变数组的长度。

¥The shift() method of Array instances removes the first element from an array and returns that removed element. This method changes the length of the array.

Try it

语法

¥Syntax

js
shift()

参数

¥Parameters

没有任何。

¥None.

返回值

¥Return value

从数组中删除的元素;如果数组为空,则为 undefined

¥The removed element from the array; undefined if the array is empty.

描述

¥Description

shift() 方法删除第 0 个索引处的元素,并将连续索引处的值向下移动,然后返回删除的值。如果 length 属性为 0,则返回 undefined

¥The shift() method removes the element at the zeroth index and shifts the values at consecutive indexes down, then returns the removed value. If the length property is 0, undefined is returned.

pop() 方法与 shift() 具有类似的行为,但应用于数组中的最后一个元素。

¥The pop() method has similar behavior to shift(), but applied to the last element in an array.

shift() 方法是 变异法 方法。它改变了 this 的长度和内容。如果你希望 this 的值相同,但返回删除第一个元素的新数组,则可以使用 arr.slice(1) 代替。

¥The shift() method is a mutating method. It changes the length and the content of this. In case you want the value of this to be the same, but return a new array with the first element removed, you can use arr.slice(1) instead.

shift() 方法是 generic。它只期望 this 值具有 length 属性和整数键控属性。虽然字符串也是类似数组的,但此方法不适合应用于它们,因为字符串是不可变的。

¥The shift() method is generic. It only expects the this value to have a length property and integer-keyed properties. Although strings are also array-like, this method is not suitable to be applied on them, as strings are immutable.

示例

¥Examples

从数组中删除一个元素

¥Removing an element from an array

以下代码显示删除第一个元素之前和之后的 myFish 数组。它还显示删除的元素:

¥The following code displays the myFish array before and after removing its first element. It also displays the removed element:

js
const myFish = ["angel", "clown", "mandarin", "surgeon"];

console.log("myFish before:", myFish);
// myFish before: ['angel', 'clown', 'mandarin', 'surgeon']

const shifted = myFish.shift();

console.log("myFish after:", myFish);
// myFish after: ['clown', 'mandarin', 'surgeon']

console.log("Removed this element:", shifted);
// Removed this element: angel

在 while 循环中使用 shift() 方法

¥Using shift() method in while loop

shift() 方法通常用在 while 循环内的条件中。在下面的示例中,每次迭代都会从数组中删除下一个元素,直到它为空:

¥The shift() method is often used in condition inside while loop. In the following example every iteration will remove the next element from an array, until it is empty:

js
const names = ["Andrew", "Tyrone", "Paul", "Maria", "Gayatri"];

while (typeof (i = names.shift()) !== "undefined") {
  console.log(i);
}
// Andrew, Tyrone, Paul, Maria, Gayatri

对非数组对象调用 shift()

¥Calling shift() on non-array objects

shift() 方法读取 thislength 属性。如果 标准化长度 为 0,则 length 再次设置为 0(而之前可能为负数或 undefined)。否则,返回 0 处的属性,其余属性左移一位。length 属性减一。

¥The shift() method reads the length property of this. If the normalized length is 0, length is set to 0 again (whereas it may be negative or undefined before). Otherwise, the property at 0 is returned, and the rest of the properties are shifted left by one. The length property is decremented by one.

js
const arrayLike = {
  length: 3,
  unrelated: "foo",
  2: 4,
};
console.log(Array.prototype.shift.call(arrayLike));
// undefined, because it is an empty slot
console.log(arrayLike);
// { '1': 4, length: 2, unrelated: 'foo' }

const plainObj = {};
// There's no length property, so the length is 0
Array.prototype.shift.call(plainObj);
console.log(plainObj);
// { length: 0 }

规范

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

¥Specifications

浏览器兼容性

BCD tables only load in the browser

¥Browser compatibility

也可以看看