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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
|
---
title: Array.prototype.every()
slug: Web/JavaScript/Reference/Global_Objects/Array/every
tags:
- Array
- ECMAScript 5
- JavaScript
- Method
- Prototype
- polyfill
translation_of: Web/JavaScript/Reference/Global_Objects/Array/every
---
<div>{{JSRef}}</div>
<p><span class="seoSummary"><strong><code>every()</code></strong> メソッドは、列内のすべての要素が指定された関数で実装されたテストに合格するかどうかをテストします。これは論理値を返します。</span></p>
<div>{{EmbedInteractiveExample("pages/js/array-every.html","shorter")}}</div>
<p class="hidden">このデモのソースファイルは GitHub リポジトリに格納されています。デモプロジェクトに協力したい場合は、<a href="https://github.com/mdn/interactive-examples">https://github.com/mdn/interactive-examples</a> をクローンしてプルリクエストを送信してください。</p>
<h2 id="Syntax" name="Syntax">構文</h2>
<pre class="syntaxbox notranslate"><var>arr</var>.every(<var>callback</var>(<var>element</var>[, <var>index</var>[, <var>array</var>]])[, <var>thisArg</var>])</pre>
<h3 id="Parameters" name="Parameters">引数</h3>
<dl>
<dt><code><var>callback</var></code></dt>
<dd>各要素に対してテストを実行する関数です。次の 3 つの引数を取ります。
<dl>
<dt><code><var>element</var></code></dt>
<dd>現在処理されている要素です。</dd>
<dt><code><var>index</var></code> {{Optional_inline}}</dt>
<dd>現在処理されている要素の添字です。</dd>
<dt><code><var>array</var></code> {{Optional_inline}}</dt>
<dd><code>every</code> が実行されている配列です。</dd>
</dl>
</dd>
<dt><code><var>thisArg</var></code> {{Optional_inline}}</dt>
<dd><code><var>callback</var></code> を実行するときに <code>this</code> として使用すされる値です。</dd>
</dl>
<h3 id="Return_value" name="Return_value">返値</h3>
<p><code><var>callback</var></code> 関数が配列のすべての要素について{{Glossary("truthy", "真値")}}を返した場合は <strong><code>true</code></strong>。それ以外は <strong><code>false</code></strong>。</p>
<h2 id="Description" name="Description">解説</h2>
<p><code>every</code> は、与えられた <code><var>callback</var></code> 関数を、配列に含まれる各要素に対して一度ずつ、<code><var>callback</var></code> が{{Glossary("falsy", "偽値")}}を返す要素が見つかるまで呼び出します。そのような要素が見つかると、<code>every</code> メソッドはただちに <code>false</code> を返します。<code><var>callback</var></code> がすべての要素に対して{{Glossary("truthy", "真値")}}を返した場合、<code>every</code> は <code>true</code> を返します。</p>
<div class="blockIndicator note">
<p><strong>注意</strong>: このメソッドを空の配列に対して呼び出すと、無条件に <code>true</code> を返します。</p>
</div>
<p><code><var>callback</var></code> は値が代入されている配列の要素に対してのみ呼び出されます。つまり、すでに削除された要素や、まだ値が代入されていない要素に対しては呼び出されません。</p>
<p><code><var>callback</var></code> は、要素の値、要素の添字、走査されている Array オブジェクトという 3 つの引数をともなって呼び出されます。</p>
<p><code><var>thisArg</var></code> 引数が <code>every</code> に与えられると、それがコールバックの <code>this</code> として使用されます。それ以外の場合は <code>undefined</code> が <code>this</code> の値として使われます。<code><var>callback</var></code> が最終的に監視できる <code>this</code> の値は、<a href="/ja/docs/Web/JavaScript/Reference/Operators/this">関数から見た <code>this</code> の決定に関する一般的なルール</a>によって決定されます。</p>
<p><code>every</code> は呼び出された配列を変化させません。</p>
<p><code>every</code> によって処理される要素の範囲は、<code><var>callback</var></code> が最初に呼び出される前に設定されます。<code><var>callback</var></code> は、<code>every</code> の呼び出しが開始された後に追加された要素に対しては、実行されません。既存の配列要素が変更されたり、削除された場合、<code><var>callback</var></code> に渡される値は <code>every</code> がそれらを訪れた時点での値になり、<code>every</code> が削除された要素を訪問することはありません。</p>
<p><code>every</code> は数学における「∀ (すべての / for all)」記号と同様のふるまいをします。具体的に言うと、空の配列に対しては <code>true</code> を返します。(<a href="https://en.wikipedia.org/wiki/Empty_set#Properties">空集合</a>のすべての要素が与えられた任意の条件を満たすことは<a href="https://en.wikipedia.org/wiki/Vacuous_truth">空虚に真</a>です。)</p>
<h2 id="Polyfill" name="Polyfill">ポリフィル</h2>
<p><code>every</code> は ECMA-262 標準に第5版で追加されたもので、この標準のそれ以外の実装には存在しないかもしれません。これを回避するには、スクリプトの最初に以下のコードを挿入することで、ネイティブで対応していない実装でも <code>every</code> を使用できるようにすることができます。</p>
<p>このアルゴリズムは、<code>Object</code> と <code>TypeError</code> が元の値を持ち、<code><var>callbackfn</var>.call</code> が {{jsxref("Function.prototype.call")}} の元の値に評価されると仮定するもので、ECMA-262 第5版で指定されているものと全く同じです。</p>
<pre class="brush: js notranslate">if (!Array.prototype.every) {
Array.prototype.every = function(callbackfn, thisArg) {
'use strict';
var T, k;
if (this == null) {
throw new TypeError('this is null or not defined');
}
// 1. Let O be the result of calling ToObject passing the this
// value as the argument.
var O = Object(this);
// 2. Let lenValue be the result of calling the Get internal method
// of O with the argument "length".
// 3. Let len be ToUint32(lenValue).
var len = O.length >>> 0;
// 4. If IsCallable(callbackfn) is false, throw a TypeError exception.
if (typeof callbackfn !== 'function' && Object.prototype.toString.call(callbackfn) !== '[object Function]') {
throw new TypeError();
}
// 5. If thisArg was supplied, let T be thisArg; else let T be undefined.
if (arguments.length > 1) {
T = thisArg;
}
// 6. Let k be 0.
k = 0;
// 7. Repeat, while k < len
while (k < len) {
var kValue;
// a. Let Pk be ToString(k).
// This is implicit for LHS operands of the in operator
// b. Let kPresent be the result of calling the HasProperty internal
// method of O with argument Pk.
// This step can be combined with c
// c. If kPresent is true, then
if (k in O) {
var testResult;
// i. Let kValue be the result of calling the Get internal method
// of O with argument Pk.
kValue = O[k];
// ii. Let testResult be the result of calling the Call internal method
// of callbackfn with T as the this value if T is not undefined
// else is the result of calling callbackfn
// and argument list containing kValue, k, and O.
if(T) testResult = callbackfn.call(T, kValue, k, O);
else testResult = callbackfn(kValue,k,O)
// iii. If ToBoolean(testResult) is false, return false.
if (!testResult) {
return false;
}
}
k++;
}
return true;
};
}
</pre>
<h2 id="Examples" name="Examples">例</h2>
<h3 id="Testing_size_of_all_array_elements" name="Testing_size_of_all_array_elements">すべての配列要素の大きさをテストする</h3>
<p>次の例は、配列内のすべての要素が 10 よりも大きいかどうかテストします。</p>
<pre class="brush: js notranslate">function isBigEnough(element, index, array) {
return element >= 10;
}
[12, 5, 8, 130, 44].every(isBigEnough); // false
[12, 54, 18, 130, 44].every(isBigEnough); // true
</pre>
<h3 id="Using_arrow_functions" name="Using_arrow_functions">アロー関数の使用</h3>
<p><a href="/ja/docs/Web/JavaScript/Reference/Functions/Arrow_functions">アロー関数</a>はより短い構文で同じテストを提供します。</p>
<pre class="brush: js notranslate">[12, 5, 8, 130, 44].every(x => x >= 10); // false
[12, 54, 18, 130, 44].every(x => x >= 10); // true</pre>
<h3 id="初期配列への影響_変更、追加、削除">初期配列への影響 (変更、追加、削除)</h3>
<p>次の例は、配列が変更されたときに <code>every</code> メソッドの動作をテストするものです。</p>
<pre class="brush: js notranslate">// ---------------
// Modifying items
// ---------------
let arr = [1, 2, 3, 4];
arr.every( (elem, index, arr) => {
arr[index+1] -= 1
console.log(`[${arr}][${index}] -> ${elem}`)
return elem < 2
})
// Loop runs for 3 iterations, but would
// have run 2 iterations without any modification
//
// 1st iteration: [1,1,3,4][0] -> 1
// 2nd iteration: [1,1,2,4][1] -> 1
// 3rd iteration: [1,1,2,3][2] -> 2
// ---------------
// Appending items
// ---------------
arr = [1, 2, 3];
arr.every( (elem, index, arr) => {
arr.push('new')
console.log(`[${arr}][${index}] -> ${elem}`)
return elem < 4
})
// Loop runs for 3 iterations, even after appending new items
//
// 1st iteration: [1, 2, 3, new][0] -> 1
// 2nd iteration: [1, 2, 3, new, new][1] -> 2
// 3rd iteration: [1, 2, 3, new, new, new][2] -> 3
// ---------------
// Deleting items
// ---------------
arr = [1, 2, 3, 4];
arr.every( (elem, index, arr) => {
arr.pop()
console.log(`[${arr}][${index}] -> ${elem}`)
return elem < 4
})
// Loop runs for 2 iterations only, as the remaining
// items are `pop()`ed off
//
// 1st iteration: [1,2,3][0] -> 1
// 2nd iteration: [1,2][1] -> 2</pre>
<h2 id="Specifications" name="Specifications">仕様書</h2>
<table class="standard-table">
<thead>
<tr>
<th scope="col">仕様書</th>
</tr>
</thead>
<tbody>
<tr>
<td>{{SpecName('ESDraft', '#sec-array.prototype.every', 'Array.prototype.every')}}</td>
</tr>
</tbody>
</table>
<h2 id="Browser_compatibility" name="Browser_compatibility">ブラウザーの互換性</h2>
<div>
<p>{{Compat("javascript.builtins.Array.every")}}</p>
</div>
<h2 id="See_also" name="See_also">関連情報</h2>
<ul>
<li>{{jsxref("Array.prototype.forEach()")}}</li>
<li>{{jsxref("Array.prototype.some()")}}</li>
<li>{{jsxref("Array.prototype.find()")}}</li>
<li>{{jsxref("TypedArray.prototype.every()")}}</li>
</ul>
|