From 33058f2b292b3a581333bdfb21b8f671898c5060 Mon Sep 17 00:00:00 2001 From: Peter Bengtsson Date: Tue, 8 Dec 2020 14:40:17 -0500 Subject: initial commit --- .../global_objects/array/includes/index.html | 137 +++++++++++++++++++++ 1 file changed, 137 insertions(+) create mode 100644 files/ja/web/javascript/reference/global_objects/array/includes/index.html (limited to 'files/ja/web/javascript/reference/global_objects/array/includes') diff --git a/files/ja/web/javascript/reference/global_objects/array/includes/index.html b/files/ja/web/javascript/reference/global_objects/array/includes/index.html new file mode 100644 index 0000000000..1354404302 --- /dev/null +++ b/files/ja/web/javascript/reference/global_objects/array/includes/index.html @@ -0,0 +1,137 @@ +--- +title: Array.prototype.includes() +slug: Web/JavaScript/Reference/Global_Objects/Array/includes +tags: + - Array + - JavaScript + - Method + - Prototype + - Reference + - inArray + - in_array + - polyfill + - メソッド +translation_of: Web/JavaScript/Reference/Global_Objects/Array/includes +--- +
{{JSRef}}
+ +

includes() メソッドは、特定の要素が配列に含まれているかどうかを true または false で返します。

+ +
{{EmbedInteractiveExample("pages/js/array-includes.html")}}
+ + + +

構文

+ +
arr.includes(valueToFind[, fromIndex])
+
+ +

引数

+ +
+
valueToFind
+
+

検索する値です。

+ +
+

メモ: 文字列や文字を比較するとき、includes()大文字と小文字を区別します

+
+
+
fromIndex {{optional_inline}}
+
この配列内で valueToFind を探し始める位置です。
+
検索される最初の文字は、fromIndex が正の値の場合は、fromIndex で見つかり、fromIndex が負の数の場合は (fromIndex の{{interwiki("wikipedia", "絶対値")}}だけ配列の末尾から文字数を戻った位置が検索開始地点となり)、fromIndex または arr.length + fromIndex で見つかります。
+
既定値は 0 です。
+
+ +

返値

+ +

{{jsxref("Boolean")}} で、truevalueToFind の値が配列内 (または、fromIndex が指定された場合はそれで示された配列の部分) から見つかった場合です。

+ +

ゼロの値はすべて、符号にかかわらず等しいとみなされます (つまり、-00+0 の両方に等しいとみなされます) が、false0 と同じとはみなされません

+ +
+

注: 技術的に言えば、includes()sameValueZero アルゴリズムを使用して、指定された要素が見つかったかどうかを確認しています。

+
+ +

+ +
[1, 2, 3].includes(2)      // true
+[1, 2, 3].includes(4)      // false
+[1, 2, 3].includes(3, 3)   // false
+[1, 2, 3].includes(3, -1)  // true
+[1, 2, NaN].includes(NaN)  // true
+
+ +

fromIndex が配列の長さと同じか大きい場合

+ +

fromIndex が配列の長さと同じか大きい場合は配列を検索せずに false を返します。

+ +
let arr = ['a', 'b', 'c']
+
+arr.includes('c', 3)    // false
+arr.includes('c', 100)  // false
+
+ +

計算値のインデックスが 0 より小さい場合

+ +

fromIndex が負の値である場合、計算値のインデックスは配列内で valueToFind の円策を開始する位置として使用するよう計算されます。計算値のインデックスが -1 * arr.length 以下の場合は、配列全体が検索されます。

+ +
// 配列の長さは 3
+// fromIndex は -100
+// 補正されたインデックスは 3 + (-100) = -97
+
+let arr = ['a', 'b', 'c']
+
+arr.includes('a', -100) // true
+arr.includes('b', -100) // true
+arr.includes('c', -100) // true
+arr.includes('a', -2)   // false
+
+ +

ジェネリックメソッドとして使用される includes()

+ +

includes() メソッドは意図的にジェネリックになっています。this が Array オブジェクトであることは必須ではないので、他の種類のオブジェクト (例えば配列風オブジェクト) にも適用することができます。

+ +

以下の例は、includes() メソッドが関数の arguments オブジェクトに対して使用される様子を示しています。

+ +
(function() {
+  console.log(Array.prototype.includes.call(arguments, 'a'))  // true
+  console.log(Array.prototype.includes.call(arguments, 'd'))  // false
+})('a','b','c') 
+ + + +

仕様

+ + + + + + + + + + + + +
仕様書
{{SpecName('ESDraft', '#sec-array.prototype.includes', 'Array.prototype.includes')}}
+ +

ブラウザーの互換性

+ +
+ + +

{{Compat("javascript.builtins.Array.includes")}}

+
+ +

関連情報

+ + -- cgit v1.2.3-54-g00ecf