aboutsummaryrefslogtreecommitdiff
path: root/files/vi/web/javascript/reference/global_objects/array/length/index.html
blob: 01186b015a3911c1c4bdf0baa9c7267537d30a10 (plain)
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
142
143
144
---
title: Array.length
slug: "Web/JavaScript/Reference/Global_Objects/Array/\blength"
translation_of: Web/JavaScript/Reference/Global_Objects/Array/length
---
<div>{{JSRef}}</div>

<p>Thuộc tính <code><strong>length</strong></code> của một mảng trả về số phần tử trong mảng đó. Đó là một số nguyên 32 bit không dấu và luôn lớn hơn chỉ mục lớn nhất của mảng (chỉ mục lớn nhất chính là dộ dài của mảng trừ đi 1).</p>

<div>{{EmbedInteractiveExample("pages/js/array-length.html")}}</div>



<h2 id="Mô_tả">Mô tả</h2>

<p><code><font face="Open Sans, arial, x-locale-body, sans-serif"><span style="background-color: #ffffff;">Giá trị hợp lệ mà </span></font>length</code> có thể biểu diễn là một số nguyên dương có miền giá trị nằm trong khoảng  2 đến 2<sup>32</sup>.</p>

<pre class="brush: js">var namelistA = new Array(4294967296); //2 to the 32nd power<sup> = </sup>4294967296
var namelistC = new Array(-100) //negative sign

console.log(namelistA.length); //RangeError: Invalid array length
console.log(namelistC.length); //RangeError: Invalid array length



var namelistB = [];
namelistB.length = Math.pow(2,32)-1; //set array length less than 2 to the 32nd power
console.log(namelistB.length);

//4294967295
</pre>

<p><code>length</code>  có thể được dùng để thay đổi số lượng phần tử có trong mảng bằng cách gán lại giá trị của <code>length</code> .  Trong ví dụ dưới đây, khi mảng chỉ có 2 phần tử nhưng ta thay đổi <code>length</code> thành 3 thì mảng sẽ tự động có thêm một phần tử mới. Tuy nhiên việc cố tình thay đổi này sẽ hình thành phần tử mới mang giá trị <code>undefined</code>.</p>

<pre class="brush: js">var arr = [1, 2, 3];
printEntries(arr);

arr.length = 5; // set array length to 5 while currently 3.
printEntries(arr);

function printEntries(arr) {
  var length = arr.length;
  for (var i = 0; i &lt; length; i++) {
    console.log(arr[i]);
  }
  console.log('=== printed ===');
}

// 1
// 2
// 3
// === printed ===
// 1
// 2
// 3
// undefined
// undefined
// === printed ===</pre>

<p>Thực sự thì bản chất của <code>length</code> property không thể hiện số phần tử 'defined' có trong mảng. Tham khảo thêm từ <a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array#Relationship_between_length_and_numerical_properties" title="Relationship between length and numerical properties">Relationship between <code>length</code> and numerical properties</a>.</p>

<p>{{js_property_attributes(1, 0, 0)}}</p>

<div>
<ul>
 <li><code>Writable</code>: Nếu thuộc tính này mang giá trị <code>false</code>, giá trị của thuộc tính sẽ không thể bị thay đổi.</li>
 <li><code>Configurable</code>: Nếu thuộc tính này mang giá trị <code>false</code>, tất cả các tác vụ cố tình thay đổi hoặc xoá như <code>Writable</code>, <code>Configurable</code>,hoặc <code>Enumerable </code>sẽ thất bại.</li>
 <li><code>Enumerable</code>: Nếu thuộc tính này mang giá trị <code>true</code>, thuộc tính có thể được duyệt thông qua các vòng lập <a href="/en-US/docs/Web/JavaScript/Reference/Statements/for">for</a> or <a href="/en-US/docs/Web/JavaScript/Reference/Statements/for...in">for..in</a>.</li>
</ul>
</div>

<h2 id="Ví_dụ">Ví dụ</h2>

<h3 id="Duyệt_mảng">Duyệt mảng</h3>

<p>Trong ví dụ sau, việc duyệt một mảng với các phần tử kiểu <code>numbers</code>  có thể được thực hiện thông qua <code>length</code>. Tại mỗi bước, giá trị của mảng được gán lại gấp đôi.</p>

<pre class="brush: js">var numbers = [1, 2, 3, 4, 5];
var length = numbers.length;
for (var i = 0; i &lt; length; i++) {
  numbers[i] *= 2;
}
// numbers is now [2, 4, 6, 8, 10]
</pre>

<h3 id="Cẳt_mảng">Cẳt mảng</h3>

<p>Trong phần mô tả ở trên, nếu <code>length</code> có thể dùng để tăng thêm số phần tử trong mảng thì ta có thể dùng <code>length </code>để cắt bớt số phần tử trong mảng. Ví dụ dưới đây minh hoạ cho việc cắt bớt 2 phần tử cuối có trong mảng 5 phần tử.</p>

<pre class="brush: js">var numbers = [1, 2, 3, 4, 5];

if (numbers.length &gt; 3) {
  numbers.length = 3;
}

console.log(numbers); // [1, 2, 3]
console.log(numbers.length); // 3
</pre>

<h2 id="Đặc_tả"> Đặc tả</h2>

<table class="standard-table">
 <tbody>
  <tr>
   <th scope="col">Đặc tả </th>
   <th scope="col">Tình trạng</th>
   <th scope="col">Ghi chú</th>
  </tr>
  <tr>
   <td>{{SpecName('ES1')}}</td>
   <td>{{Spec2('ES1')}}</td>
   <td>Định nghĩa lần đâu</td>
  </tr>
  <tr>
   <td>{{SpecName('ES5.1', '#sec-15.4.5.2', 'Array.length')}}</td>
   <td>{{Spec2('ES5.1')}}</td>
   <td></td>
  </tr>
  <tr>
   <td>{{SpecName('ES6', '#sec-properties-of-array-instances-length', 'Array.length')}}</td>
   <td>{{Spec2('ES6')}}</td>
   <td></td>
  </tr>
  <tr>
   <td>{{SpecName('ESDraft', '#sec-properties-of-array-instances-length', 'Array.length')}}</td>
   <td>{{Spec2('ESDraft')}}</td>
   <td></td>
  </tr>
 </tbody>
</table>

<h2 id="Tính_tương_thích">Tính tương thích</h2>

<div>


<p>{{Compat("javascript.builtins.Array.length")}}</p>
</div>

<h2 id="Liên_quan">Liên quan</h2>

<ul>
 <li>{{jsxref("Array")}}</li>
</ul>