--- title: Array.prototype.shift() slug: Web/JavaScript/Reference/Global_Objects/Array/shift tags: - Array - JavaScript - Method - Mảng - Nguyên mẫu - Prototype - hàm translation_of: Web/JavaScript/Reference/Global_Objects/Array/shift ---
Hàm shift()
sẽ xóa phần tử đầu tiên của một mảng và trả về phần tử đó. Hàm này sau khi thực thi sẽ làm thay đổi kích thước của mảng bị tác động.
arr.shift()
Phần tử đầu tiên của mảng.
Hàm shift
sẽ xóa phần tử ở vị trí 0 trong mảng và dịch vị trí của các phần tử tiếp theo xuống, sau đó trả về giá trị của phần tử bị xóa. Nếu giá trị của thuộc tính {{jsxref("Array.length", "length")}} bằng 0, giá trị {{jsxref("undefined")}} được trả về.
shift
is intentionally generic; this method can be {{jsxref("Function.call", "called", "", 1)}} or {{jsxref("Function.apply", "applied", "", 1)}} to objects resembling arrays. Objects which do not contain a length
property reflecting the last in a series of consecutive, zero-based numerical properties may not behave in any meaningful manner.
Đoạn mã code dưới đây hiển thị mảng myFish
trước và sau khi xóa bỏ phần tử đầu tiên của mảng đó. Nó cũng hiển thị phần tử bị xóa bỏ:
var myFish = ['angel', 'clown', 'mandarin', 'surgeon']; console.log('myFish before: ' + 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"
Đặc tả | Trạng thái | Chú thích |
---|---|---|
{{SpecName('ES3')}} | {{Spec2('ES3')}} | Initial definition. Đã cài đặt từ phiên bản JavaScript 1.2. |
{{SpecName('ES5.1', '#sec-15.4.4.9', 'Array.prototype.shift')}} | {{Spec2('ES5.1')}} | |
{{SpecName('ES6', '#sec-array.prototype.shift', 'Array.prototype.shift')}} | {{Spec2('ES6')}} | |
{{SpecName('ESDraft', '#sec-array.prototype.shift', 'Array.prototype.shift')}} | {{Spec2('ESDraft')}} |
Tính năng | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|
Đã hỗ trợ | {{CompatChrome("1.0")}} | {{CompatGeckoDesktop("1.7")}} | {{CompatIE("5.5")}} | {{CompatVersionUnknown}} | {{CompatVersionUnknown}} |
Tính năng | Android | Chrome for Android | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
---|---|---|---|---|---|---|
Đã hỗ trợ | {{CompatVersionUnknown}} | {{CompatVersionUnknown}} | {{CompatVersionUnknown}} | {{CompatVersionUnknown}} | {{CompatVersionUnknown}} | {{CompatVersionUnknown}} |