diff options
author | Masahiro FUJIMOTO <mfujimot@gmail.com> | 2021-08-16 09:54:35 +0900 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-08-16 09:54:35 +0900 |
commit | 816ac814e8a11897db45dfad83cfdd9d3dae55fb (patch) | |
tree | 2f3f1bd89c6ad7711a2b02dc00c240c7e6d07e2f /files/ja/web/javascript/reference/errors/is_not_iterable | |
parent | 4ad4ec00bd93c2344f9e8141508dce0baf22e00a (diff) | |
download | translated-content-816ac814e8a11897db45dfad83cfdd9d3dae55fb.tar.gz translated-content-816ac814e8a11897db45dfad83cfdd9d3dae55fb.tar.bz2 translated-content-816ac814e8a11897db45dfad83cfdd9d3dae55fb.zip |
Web/JavaScript/Reference/Errors以下の5文書をMarkdown化 (#1971)
- 5文書をMarkdown化
- 2021/08/07時点の最新版に同期
Diffstat (limited to 'files/ja/web/javascript/reference/errors/is_not_iterable')
-rw-r--r-- | files/ja/web/javascript/reference/errors/is_not_iterable/index.html | 131 | ||||
-rw-r--r-- | files/ja/web/javascript/reference/errors/is_not_iterable/index.md | 136 |
2 files changed, 136 insertions, 131 deletions
diff --git a/files/ja/web/javascript/reference/errors/is_not_iterable/index.html b/files/ja/web/javascript/reference/errors/is_not_iterable/index.html deleted file mode 100644 index 665371733f..0000000000 --- a/files/ja/web/javascript/reference/errors/is_not_iterable/index.html +++ /dev/null @@ -1,131 +0,0 @@ ---- -title: 'TypeError: ''x'' is not iterable' -slug: Web/JavaScript/Reference/Errors/is_not_iterable -tags: - - Error - - JavaScript - - Reference - - TypeError -translation_of: Web/JavaScript/Reference/Errors/is_not_iterable ---- -<div>{{jsSidebar("Errors")}}</div> - -<p>JavaScript の例外 "is not iterable" は、 <a href="/ja/docs/Web/JavaScript/Guide/Loops_and_iteration#for...of_statement">for…of</a> の右辺として与えられた値や、 {{jsxref("Promise.all")}} または {{jsxref("TypedArray.from")}} のような関数の引数として与えられた値が<a href="/ja/docs/Web/JavaScript/Reference/Iteration_protocols">反復可能オブジェクト</a>ではなかった場合に発生します。</p> - -<h2 id="Message">エラーメッセージ</h2> - -<pre class="brush: js">TypeError: 'x' is not iterable (Firefox, Chrome) -TypeError: 'x' is not a function or its return value is not iterable (Chrome) -</pre> - -<h2 id="エラータイプ">エラータイプ</h2> - -<p>{{jsxref("TypeError")}}</p> - -<h2 id="何がうまくいかなかったのか?">何がうまくいかなかったのか?</h2> - -<p><a href="/ja/docs/Web/JavaScript/Guide/Loops_and_iteration#for...of_statement">for…of</a> の右辺、 {{jsxref("Promise.all")}} や {{jsxref("TypedArray.from")}} などの引数として指定された値が<a href="/ja/docs/Web/JavaScript/Reference/Iteration_protocols">反復可能オブジェクト</a>ではありません。反復可能なものは、 {{jsxref("Array")}}, {{jsxref("String")}}, {{jsxref("Map")}} 等のような組み込み反復可能型や、ジェネレーターの結果、<a href="/ja/docs/Web/JavaScript/Reference/Iteration_protocols#the_iterable_protocol">反復可能プロトコル</a>を実装しているオブジェクトが成ることができます。</p> - -<h2 id="例">例</h2> - -<h3 id="Iterating_over_Object_properties">オブジェクトのプロパティの反復処理</h3> - -<p>JavaScript では、 {{jsxref("Object")}} は<a href="/ja/docs/Web/JavaScript/Reference/Iteration_protocols#the_iterable_protocol">反復処理プロトコル</a> を実装していない限り反復処理できません。したがって、オブジェクトのプロパティを反復処理するために <a href="/ja/docs/Web/JavaScript/Guide/Loops_and_iteration#for...of_statement">for…of</a> を使用することはできません。</p> - -<pre class="brush: js example-bad">var obj = { 'France': 'Paris', 'England': 'London' }; -for (let p of obj) { // TypeError: obj is not iterable - // … -} -</pre> - -<p>代わりに、オブジェクトのプロパティを反復処理するためには {{jsxref("Object.keys")}} か {{jsxref("Object.entries")}} を使用してください。</p> - -<pre class="brush: js example-good">var obj = { 'France': 'Paris', 'England': 'London' }; -// Iterate over the property names: -for (let country of Object.keys(obj)) { - var capital = obj[country]; - console.log(country, capital); -} - -for (const [country, capital] of Object.entries(obj)) - console.log(country, capital); - -</pre> - -<p>この使用例のそのほかの選択肢として、{{jsxref("Map")}} を使用することもできます。</p> - -<pre class="brush: js example-good">var map = new Map; -map.set('France', 'Paris'); -map.set('England', 'London'); -// Iterate over the property names: -for (let country of map.keys()) { - let capital = map[country]; - console.log(country, capital); -} - -for (let capital of map.values()) - console.log(capital); - -for (const [country, capital] of map.entries()) - console.log(country, capital); -</pre> - -<h3 id="Iterating_over_a_generator">ジェネレーターを反復処理する</h3> - -<p><a href="/ja/docs/Web/JavaScript/Guide/Iterators_and_Generators#generators">ジェネレーター</a> 反復可能オブジェクトを生成するために呼び出す関数です。</p> - -<pre class="brush: js example-bad">function* generate(a, b) { - yield a; - yield b; -} - -for (let x of generate) // TypeError: generate is not iterable - console.log(x); -</pre> - -<p>ジェネレーターを呼び出していないとき、ジェネレーターに対応した {{jsxref("Function")}} オブジェクトは呼び出し可能ですが、反復処理はできません。ジェネレーターを呼び出すと、ジェネレーターの実行中に生成された値を反復処理する反復可能オブジェクトが生成されます。</p> - -<pre class="brush: js example-good">function* generate(a, b) { - yield a; - yield b; -} - -for (let x of generate(1,2)) - console.log(x); -</pre> - -<h3 id="Iterating_over_a_custom_iterable">独自の反復可能オブジェクトでの反復処理</h3> - -<p>独自の反復可能オブジェクトは、 {{jsxref("Symbol.iterator")}} メソッドを実装することで作成することができます。 iterator メソッドはイテレーターであるオブジェクト、すなわち next メソッドを持っている必要があります。 -</p> - -<pre class="brush: js example-bad">const myEmptyIterable = { - [Symbol.iterator]() { - return [] // [] は反復可能ですが、イテレーターではありません。 -- next メソッドがないからです。 - } -} - -Array.from(myEmptyIterable); // TypeError: myEmptyIterable is not iterable -</pre> - -<p>こちらは正しい実装です。</p> - -<pre class="brush: js example-good">const myEmptyIterable = { - [Symbol.iterator]() { - return [][Symbol.iterator]() - } -} - -Array.from(myEmptyIterable); // [] -</pre> - -<h2 id="See_also">関連情報</h2> - -<ul> - <li><a href="/ja/docs/Web/JavaScript/Reference/Iteration_protocols#the_iterable_protocol">反復処理プロトコル</a></li> - <li>{{jsxref("Object.keys")}}</li> - <li>{{jsxref("Object.entries")}}</li> - <li>{{jsxref("Map")}}</li> - <li><a href="/ja/docs/Web/JavaScript/Guide/Iterators_and_Generators#generators">ジェネレーター</a></li> - <li><a href="/ja/docs/Web/JavaScript/Guide/Loops_and_iteration#for...of_statement">for…of</a></li> -</ul> diff --git a/files/ja/web/javascript/reference/errors/is_not_iterable/index.md b/files/ja/web/javascript/reference/errors/is_not_iterable/index.md new file mode 100644 index 0000000000..3b3ffb43aa --- /dev/null +++ b/files/ja/web/javascript/reference/errors/is_not_iterable/index.md @@ -0,0 +1,136 @@ +--- +title: 'TypeError: ''x'' is not iterable' +slug: Web/JavaScript/Reference/Errors/is_not_iterable +tags: + - Error + - JavaScript + - Reference + - TypeError +translation_of: Web/JavaScript/Reference/Errors/is_not_iterable +--- +{{jsSidebar("Errors")}} + +JavaScript の例外 "is not iterable" は、 [for…of](/ja/docs/Web/JavaScript/Guide/Loops_and_iteration#for...of_statement) の右辺として与えられた値や、 {{jsxref("Promise.all")}} または {{jsxref("TypedArray.from")}} のような関数の引数として与えられた値が[反復可能オブジェクト](/ja/docs/Web/JavaScript/Reference/Iteration_protocols)ではなかった場合に発生します。 + +## エラーメッセージ + +```js +TypeError: 'x' is not iterable (Firefox, Chrome) +TypeError: 'x' is not a function or its return value is not iterable (Chrome) +``` + +## エラー種別 + +{{jsxref("TypeError")}} + +## エラーの原因 + +[for…of](/ja/docs/Web/JavaScript/Guide/Loops_and_iteration#for...of_statement) の右辺、 {{jsxref("Promise.all")}} や {{jsxref("TypedArray.from")}} などの引数として指定された値が[反復可能オブジェクト](/ja/docs/Web/JavaScript/Reference/Iteration_protocols)ではありません。反復可能なものは、{{jsxref("Array")}}、{{jsxref("String")}}、{{jsxref("Map")}} 等のような組み込み反復可能型や、ジェネレーターの結果、[反復可能プロトコル](/ja/docs/Web/JavaScript/Reference/Iteration_protocols#the_iterable_protocol)を実装しているオブジェクトが成ることができます。</p> + +## 例 + +### オブジェクトのプロパティの反復処理 + +JavaScript では、 {{jsxref("Object")}} は[反復可能プロトコル](/ja/docs/Web/JavaScript/Reference/Iteration_protocols#the_iterable_protocol)を実装していない限り反復処理できません。したがって、オブジェクトのプロパティを反復処理するために [for…of](/ja/docs/Web/JavaScript/Guide/Loops_and_iteration#for...of_statement) を使用することはできません。</p> + +```js example-bad +var obj = { 'France': 'Paris', 'England': 'London' }; +for (let p of obj) { // TypeError: obj is not iterable + // … +} +``` + +代わりに、オブジェクトのプロパティを反復処理するためには {{jsxref("Object.keys")}} か {{jsxref("Object.entries")}} を使用してください。 + +```js example-good +var obj = { 'France': 'Paris', 'England': 'London' }; +// Iterate over the property names: +for (let country of Object.keys(obj)) { + var capital = obj[country]; + console.log(country, capital); +} + +for (const [country, capital] of Object.entries(obj)) + console.log(country, capital); +``` + +この使用例のそのほかの選択肢として、{{jsxref("Map")}} を使用することもできます。 + +```js example-good +var map = new Map; +map.set('France', 'Paris'); +map.set('England', 'London'); +// Iterate over the property names: +for (let country of map.keys()) { + let capital = map[country]; + console.log(country, capital); +} + +for (let capital of map.values()) + console.log(capital); + +for (const [country, capital] of map.entries()) + console.log(country, capital); +``` + +### ジェネレーターの反復処理 + +[ジェネレーター](/ja/docs/Web/JavaScript/Guide/Iterators_and_Generators#generators)は反復可能オブジェクトを生成するために呼び出す関数です。</p> + +```js example-bad +function* generate(a, b) { + yield a; + yield b; +} + +for (let x of generate) // TypeError: generate is not iterable + console.log(x); +``` + +ジェネレーターを呼び出していないとき、ジェネレーターに対応した {{jsxref("Function")}} オブジェクトは呼び出し可能ですが、反復処理はできません。ジェネレーターを呼び出すと、ジェネレーターの実行中に生成された値を反復処理する反復可能オブジェクトが生成されます。 + +```js example-good +function* generate(a, b) { + yield a; + yield b; +} + +for (let x of generate(1,2)) + console.log(x); +``` + +### 独自の反復可能オブジェクトでの反復処理 + +独自の反復可能オブジェクトは、 {{jsxref("Symbol.iterator")}} メソッドを実装することで作成することができます。 iterator メソッドはイテレーターであるオブジェクト、すなわち next メソッドを返す必要があります。 + +```js example-bad +const myEmptyIterable = { + [Symbol.iterator]() { + return [] // [] は反復可能ですが、イテレーターではありません。 -- next メソッドがないからです。 + } +} + +Array.from(myEmptyIterable); // TypeError: myEmptyIterable is not iterable +``` + +こちらは正しい実装です。 + +```js example-good +const myEmptyIterable = { + [Symbol.iterator]() { + return [][Symbol.iterator]() + } +} + +Array.from(myEmptyIterable); // [] +``` + +## 関連情報 + +- [反復可能プロトコル](/ja/docs/Web/JavaScript/Reference/Iteration_protocols#the_iterable_protocol) +- {{jsxref("Object.keys")}} +- {{jsxref("Object.entries")}} +- {{jsxref("Map")}} +- [ジェネレーター](/ja/docs/Web/JavaScript/Guide/Iterators_and_Generators#generators) +- [for…of](/ja/docs/Web/JavaScript/Guide/Loops_and_iteration#for...of_statement) +</ul> |