1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
|
---
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
---
<div>{{JSRef}}</div>
<p>Hàm <code><strong>shift()</strong></code> sẽ xóa <strong>phần tử đầu tiên</strong> 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.</p>
<h2 id="Cú_pháp">Cú pháp</h2>
<pre class="syntaxbox notranslate"><code><var>arr</var>.shift()</code></pre>
<h3 id="Giá_trị_trả_về">Giá trị trả về</h3>
<p>Phần tử đầu tiên của mảng.</p>
<h2 id="Mô_tả">Mô tả</h2>
<p>Hàm <code>shift</code> 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ề.</p>
<p><code>shift</code> 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 <code>length</code> property reflecting the last in a series of consecutive, zero-based numerical properties may not behave in any meaningful manner.</p>
<h2 id="Ví_dụ">Ví dụ</h2>
<h3 id="Xóa_bỏ_một_phần_tử_khỏi_một_mảng">Xóa bỏ một phần tử khỏi một mảng</h3>
<p>Đoạn mã code dưới đây hiển thị mảng <code>myFish</code> 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ỏ:</p>
<pre class="brush: js notranslate">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"</pre>
<h2 id="Đặc_tả">Đặc tả</h2>
<table class="standard-table">
<tbody>
<tr>
<th scope="col">Đặc tả</th>
<th scope="col">Trạng thái</th>
<th scope="col">Chú thích</th>
</tr>
<tr>
<td>{{SpecName('ES3')}}</td>
<td>{{Spec2('ES3')}}</td>
<td>Initial definition. Đã cài đặt từ phiên bản JavaScript 1.2.</td>
</tr>
<tr>
<td>{{SpecName('ES5.1', '#sec-15.4.4.9', 'Array.prototype.shift')}}</td>
<td>{{Spec2('ES5.1')}}</td>
<td></td>
</tr>
<tr>
<td>{{SpecName('ES6', '#sec-array.prototype.shift', 'Array.prototype.shift')}}</td>
<td>{{Spec2('ES6')}}</td>
<td></td>
</tr>
<tr>
<td>{{SpecName('ESDraft', '#sec-array.prototype.shift', 'Array.prototype.shift')}}</td>
<td>{{Spec2('ESDraft')}}</td>
<td></td>
</tr>
</tbody>
</table>
<h2 id="Tương_thích_với_trình_duyệt">Tương thích với trình duyệt</h2>
<div>{{CompatibilityTable}}</div>
<div id="compat-desktop">
<table class="compat-table">
<tbody>
<tr>
<th>Tính năng</th>
<th>Chrome</th>
<th>Firefox (Gecko)</th>
<th>Internet Explorer</th>
<th>Opera</th>
<th>Safari</th>
</tr>
<tr>
<td>Đã hỗ trợ</td>
<td>{{CompatChrome("1.0")}}</td>
<td>{{CompatGeckoDesktop("1.7")}}</td>
<td>{{CompatIE("5.5")}}</td>
<td>{{CompatVersionUnknown}}</td>
<td>{{CompatVersionUnknown}}</td>
</tr>
</tbody>
</table>
</div>
<div id="compat-mobile">
<table class="compat-table">
<tbody>
<tr>
<th>Tính năng</th>
<th>Android</th>
<th>Chrome for Android</th>
<th>Firefox Mobile (Gecko)</th>
<th>IE Mobile</th>
<th>Opera Mobile</th>
<th>Safari Mobile</th>
</tr>
<tr>
<td>Đã hỗ trợ</td>
<td>{{CompatVersionUnknown}}</td>
<td>{{CompatVersionUnknown}}</td>
<td>{{CompatVersionUnknown}}</td>
<td>{{CompatVersionUnknown}}</td>
<td>{{CompatVersionUnknown}}</td>
<td>{{CompatVersionUnknown}}</td>
</tr>
</tbody>
</table>
</div>
<h2 id="Xem_thêm_các_mục_tương_tự">Xem thêm các mục tương tự</h2>
<ul>
<li>{{jsxref("Array.prototype.push()")}}</li>
<li>{{jsxref("Array.prototype.pop()")}}</li>
<li>{{jsxref("Array.prototype.unshift()")}}</li>
</ul>
|