Movatterモバイル変換


[0]ホーム

URL:


MDN Web Docs

TypeError: 'x' is not iterable

The JavaScript exception "is not iterable" occurs when the value which isspread into an array or function call, given as theright-hand side offor...of,as argument of a function such asPromise.all orSet(),or as the right-hand side of an arraydestructuring,is not aniterable object. This error is also encountered whenArray.fromAsync() orfor await...of is used with anon-async iterable.

Message

TypeError: Spread syntax requires ...iterable[Symbol.iterator] to be a function (V8-based & Safari)TypeError: %Array%.from requires that the property of the first argument, items[Symbol.iterator], when exists, be a function (V8-based & Safari)TypeError: Array.fromAsync requires that the property of the first argument, items[Symbol.asyncIterator], when exists, be a function (V8-based & Safari)TypeError: object is not iterable (cannot read property Symbol(Symbol.iterator)) (V8-based)TypeError: x is not async iterable (V8-based)TypeError: x is not iterable (V8-based & Firefox)TypeError: undefined is not a function (near '...y of x...') (Safari)TypeError: Array.from: no function (Safari)TypeError: Type error (Safari)

Error type

What went wrong?

The value which isspread into an array or function call, given as the right-hand side offor...of,or as argument of a function such asPromise.all orSet(),or as the source of an arraydestructuring pattern,is not aniterable object.An iterable can be a built-in iterable type such asArray,String orMap, a generator result, oran object implementing theiterable protocol.

js
const nonIterable1 = {};const nonIterable2 = { [Symbol.iterator]: 1 };[...nonIterable1];Math.max(...nonIterable1);for (const x of nonIterable1);new Set(nonIterable1);Array.from(nonIterable2);new Int8Array(nonIterable2);const [] = nonIterable1;

Examples

Array destructuring a non-iterable

js
const myObj = { arrayOrObjProp1: {}, arrayOrObjProp2: [42] };const {  arrayOrObjProp1: [value1],  arrayOrObjProp2: [value2],} = myObj; // TypeError: object is not iterableconsole.log(value1, value2);

The non-iterable might turn to beundefined in some runtime environments.

Iterating over Object properties

In JavaScript,Objects are not iterable unless they implementtheiterable protocol.Therefore, you cannot usefor...ofto iterate over the properties of an object.

js
const obj = { France: "Paris", England: "London" };for (const p of obj) {  // …} // TypeError: obj is not iterable

Instead you have to useObject.keys orObject.entries, toiterate over the properties or entries of an object.

js
const obj = { France: "Paris", England: "London" };// Iterate over the property names:for (const country of Object.keys(obj)) {  const capital = obj[country];  console.log(country, capital);}for (const [country, capital] of Object.entries(obj)) {  console.log(country, capital);}

Another option for this use case might be to use aMap:

js
const map = new Map();map.set("France", "Paris");map.set("England", "London");// Iterate over the property names:for (const country of map.keys()) {  const capital = map.get(country);  console.log(country, capital);}for (const capital of map.values()) {  console.log(capital);}for (const [country, capital] of map.entries()) {  console.log(country, capital);}

Iterating over a generator

Generator functionsare functions you call to produce an iterable object.

js
function* generate(a, b) {  yield a;  yield b;}for (const x of generate) {  console.log(x);} // TypeError: generate is not iterable

When they are not called, theFunction object corresponding to thegenerator is callable, but not iterable. Calling a generator produces an iterableobject which will iterate over the values yielded during the execution of thegenerator.

js
function* generate(a, b) {  yield a;  yield b;}for (const x of generate(1, 2)) {  console.log(x);}

Iterating over a custom iterable

Custom iterables can be created by implementing theSymbol.iterator method. You must be certain that your iterator methodreturns an object which is an iterator, which is to say it must have a next method.

js
const myEmptyIterable = {  [Symbol.iterator]() {    return []; // [] is iterable, but it is not an iterator — it has no next method.  },};Array.from(myEmptyIterable); // TypeError: myEmptyIterable is not iterable

Here is a correct implementation:

js
const myEmptyIterable = {  [Symbol.iterator]() {    return [][Symbol.iterator]();  },};Array.from(myEmptyIterable); // []

See also

Help improve MDN

Learn how to contribute.

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp