aboutsummaryrefslogtreecommitdiff
path: root/files/zh-tw/web/javascript/reference/global_objects/array/length/index.html
blob: 6f13a69ecd32bf044a6e62b4e3ba6f65806a5175 (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
---
title: Array.length
slug: Web/JavaScript/Reference/Global_Objects/Array/length
translation_of: Web/JavaScript/Reference/Global_Objects/Array/length
---
<div>{{JSRef}}</div>

<p><code><strong>length</strong></code> 為<code>Array物件的屬性</code> ,可供設定或回傳該陣列實體中包含的元素個數。其值必為一大於零、32位元、且恆大於該陣列最大索引數的正整數。</p>

<pre class="brush: js">var items = ['shoes', 'shirts', 'socks', 'sweaters'];
items.length;

// returns 4</pre>

<h2 id="描述">描述</h2>

<p><code>length</code> 屬性的值必為一正整數,其值必介於 0 ~ 2^32 (不包含)之間.</p>

<pre class="brush: js">var namelistA = new Array(4294967296); //2^32 = 4294967296
var namelistC = new Array(-100) //負數

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; //將長度設定介於 0 ~ 2^32 -1
console.log(namelistB.length);

//4294967295</pre>

<p>你可以透過改變 <code>length</code> 屬性來改變陣列的長度。當你透過 <code>length</code> 屬性來增加陣列的長度時,陣列中實際的元素也會隨之增加。舉例來說,當你將 array.length 由 2 增加為3,則改動後該陣列即擁有3個元素,該新增的元素則會是一個不可迭代(non-iterable)的空槽(empty slot)。</p>

<pre>const arr = [1, 2];
console.log(arr);
// [ 1, 2 ]

arr.length = 5; // 將arr的length由2改成5
console.log(arr);
// [ 1, 2, &lt;3 empty items&gt; ]

arr.forEach(element =&gt; console.log(element)); // 空元素無法被迭代
// 1
// 2</pre>

<p>如上所見,<code>length</code> 屬性不盡然代表陣列中所有已定義的元素個數。詳見 <a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array#Relationship_between_length_and_numerical_properties" title="Relationship between length and numerical properties">length 與數值屬性的關係</a></p>

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

<div>
<ul>
 <li><code>Writable</code>: 如果此屬性值為<code>false</code>,則該屬性的內容值無法被改動。</li>
 <li><code>Configurable</code>: 如果此屬性值為<code>false</code>,任何刪除屬性或更改其屬性的操作(<code>Writable</code>, <code>Configurable</code>, or <code>Enumerable</code>)皆會失敗。</li>
 <li><code>Enumerable</code>: 如果此屬性值為<code>true</code>,該內容值可倍 <a href="/en-US/docs/Web/JavaScript/Reference/Statements/for">for</a> 或 <a href="/en-US/docs/Web/JavaScript/Reference/Statements/for...in">for..in</a> 迴圈迭代處理。</li>
</ul>
</div>

<h2 id="範例">範例</h2>

<h3 id="對陣列進行迭代處理">對陣列進行迭代處理</h3>

<p>以下範例中, 陣列 <code>numbers</code> 透過 <code>length</code> 屬性進行迭代操作,並將其內容值加倍。</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 內容值變為 [2, 4, 6, 8, 10]
</pre>

<h3 id="縮減陣列">縮減陣列</h3>

<p>以下範例中, 陣列 <code>numbers</code>  的長度若大於 3,則將其長度縮減至 3。</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="規格">規格</h2>

<table class="standard-table">
 <tbody>
  <tr>
   <th scope="col">Specification</th>
   <th scope="col">Status</th>
   <th scope="col">Comment</th>
  </tr>
  <tr>
   <td>{{SpecName('ES1')}}</td>
   <td>{{Spec2('ES1')}}</td>
   <td>Initial definition.</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="瀏覽器相容性">瀏覽器相容性</h2>

<div>


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

<h2 id="其他">其他</h2>

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