From 8f6ecaf59da51850f51be0cedefe3d0097534678 Mon Sep 17 00:00:00 2001 From: Masahiro FUJIMOTO Date: Wed, 22 Sep 2021 00:13:53 +0900 Subject: Global_Objects/Array/includes を更新 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Markdown へ変換 - 2021/08/03 時点の英語版に同期 --- .../global_objects/array/includes/index.md | 159 ++++++++++----------- 1 file changed, 72 insertions(+), 87 deletions(-) (limited to 'files/ja/web/javascript/reference/global_objects') diff --git a/files/ja/web/javascript/reference/global_objects/array/includes/index.md b/files/ja/web/javascript/reference/global_objects/array/includes/index.md index b78bb5d20e..e96597dcd1 100644 --- a/files/ja/web/javascript/reference/global_objects/array/includes/index.md +++ b/files/ja/web/javascript/reference/global_objects/array/includes/index.md @@ -4,79 +4,80 @@ slug: Web/JavaScript/Reference/Global_Objects/Array/includes tags: - Array - JavaScript - - Method - - Prototype - - Reference + - メソッド + - プロトタイプ + - リファレンス - inArray - in_array - - polyfill - - メソッド + - ポリフィル +browser-compat: javascript.builtins.Array.includes translation_of: Web/JavaScript/Reference/Global_Objects/Array/includes --- -
{{JSRef}}
+{{JSRef}} + +**`includes()`** メソッドは、特定の要素が配列に含まれているかどうかを `true` または `false` で返します。 + +{{EmbedInteractiveExample("pages/js/array-includes.html")}} -

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

+## 構文 -
{{EmbedInteractiveExample("pages/js/array-includes.html")}}
+```js +includes(searchElement) +includes(searchElement, fromIndex) +``` - +### 引数 -

構文

+- `searchElement` -
arr.includes(valueToFind[, fromIndex])
-
+ - : 検索する値です。 -

引数

+ > **Note:** 文字列や文字を比較する場合、`includes()` は*大文字と小文字を区別します*。 -
-
valueToFind
-
-

検索する値です。

+- `fromIndex` {{optional_inline}} -
-

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

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

返値

+ 検索される最初の要素は、`fromIndex` が正の値の場合は `fromIndex` の位置に、`fromIndex` が負の値の場合は `arr.length + fromIndex` の位置になります (検索を開始する配列の端からの要素数として fromIndex の{{interwiki("wikipedia", "絶対値")}}を使用します)。 -

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

+ 既定値は `0` です。 -

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

+### 返値 -
-

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

-
+論理型で、`searchElement` の値が配列内 (`fromIndex` が指定されていた場合は、配列のその位置以降の部分) にあった場合は `true` を返します。 -

+ゼロの値は符号に関わらず、すべて等しいとみなされます (すなわち、`-0` は `0` とも `+0` 友等しいとみなされます) が、`false` は `0` と同じとはみなされ*ません*。 -
[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
-
+> **Note:** 技術的に言えば、`includes()` は [`sameValueZero`](/ja/docs/Web/JavaScript/Equality_comparisons_and_sameness#same-value-zero_equality) アルゴリズムを使用して、指定された要素が見つかったかどうかを確認しています。 -

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

+## 例 -

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

+```js +[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 +["1", "2", "3"].includes(3) // false +``` -
let arr = ['a', 'b', 'c']
+### `fromIndex` が配列の長さと同じか大きい場合
+
+`fromIndex` が配列の長さと同じか大きい場合は、配列を検索せずに `false` を返します。
+
+```js
+let arr = ['a', 'b', 'c']
 
 arr.includes('c', 3)    // false
 arr.includes('c', 100)  // false
-
+``` -

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

+### 計算された位置が 0 より小さい場合 -

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

+`fromIndex` が負の値である場合、`searchElement` の検索を開始するための配列内の位置として、計算により位置が算出されます。計算された位置が `0` 以下の場合は、配列全体が検索されます。 -
// 配列の長さは 3
+```js
+// 配列の長さは 3
 // fromIndex は -100
 // 補正されたインデックスは 3 + (-100) = -97
 
@@ -86,50 +87,34 @@ arr.includes('a', -100) // true
 arr.includes('b', -100) // true
 arr.includes('c', -100) // true
 arr.includes('a', -2)   // false
-
+``` -

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

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

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

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

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

+以下の例は、`includes()` メソッドが関数の [arguments](/ja/docs/Web/JavaScript/Reference/Functions/arguments) オブジェクトに対して使用される様子を示しています。 -
(function() {
+```js
+(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")}}

-
- -

関連情報

- - +})('a','b','c') +``` + +## 仕様書 + +{{Specifications}} + +## ブラウザーの互換性 + +{{Compat}} + +## 関連情報 + +- `Array.prototype.includes` のポリフィルは [`core-js`](https://github.com/zloirock/core-js#ecmascript-array) で利用できます +- {{jsxref("TypedArray.prototype.includes()")}} +- {{jsxref("String.prototype.includes()")}} +- {{jsxref("Array.prototype.indexOf()")}} +- {{jsxref("Array.prototype.find()")}} +- {{jsxref("Array.prototype.findIndex()")}} -- cgit v1.2.3-54-g00ecf