array.shift
shift()
方法从数组中删除第一个元素,并返回该元素的值。此方法更改数组的长度。
var a = [1, 2, 3];
var b = a.shift();
console.log(a); // [2, 3]
console.log(b); // 1
语法
arr.shift()
返回值
从数组中删除的元素; 如果数组为空则返回undefined
。
描述
示例
移除数组中的一个元素
以下代码显示了删除其第一个元素之前和之后的myFish数组。它还显示已删除的元素:
var myFish = ['angel', 'clown', 'mandarin', 'surgeon'];
console.log('myFish before:', JSON.stringify(myFish));
// myFish before: ['angel', 'clown', 'mandarin', 'surgeon']
var shifted = myFish.shift();
console.log('myFish after:', myFish);
// myFish after: ['clown', 'mandarin', 'surgeon']
console.log('Removed this element:', shifted);
// Removed this element: angel
规范
Specification | Status | Comment |
---|---|---|
ECMAScript 3rd Edition (ECMA-262) | Standard | Initial definition. Implemented in JavaScript 1.2. |
ECMAScript 5.1 (ECMA-262)The definition of 'Array.prototype.shift' in that specification. | Standard | |
ECMAScript 2015 (6th Edition, ECMA-262)The definition of 'Array.prototype.shift' in that specification. | Standard | |
ECMAScript Latest Draft (ECMA-262)The definition of 'Array.prototype.shift' in that specification. | Living Standard | |
浏览器兼容性
Feature | Chrome | Edge | Firefox | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|---|
Basic Support | 1 | (Yes) | 1 | 5.5 | (Yes) | (Yes) |
Feature | Android | Chrome for Android | Edge mobile | Firefox for Android | IE mobile | Opera Android | iOS Safari |
---|---|---|---|---|---|---|---|
Basic Support | (Yes) | (Yes) | (Yes) | 1 | (Yes) | (Yes) | (Yes) |
本文档系腾讯云开发者社区成员共同维护,如有问题请联系 cloudcommunity@tencent.com