This page was translated from English by the community.Learn more and join the MDN Web Docs community.
TypeError: 'x' is not iterable
메시지
TypeError: 'x' is not iterable (Firefox, Chrome) TypeError: 'x' is not a function or its return value is not iterable (Chrome)
In this article
에러 타입
TypeError무엇이 문제인가요?
Promise.all 또는TypedArray.from 과 같은 함수의 아규먼트 또는for…of 의 right hand-side 로 주어진 값이iterable 객체가 아닙니다. iterable 은Array,String 또는Map, 생성자 결과, 또는iterable protocol 구현 객체와 같은 내장 iterable 타입이 될 수 있습니다.
예제
>모든 객체 프로퍼티 iterating
JavaScript 에서iterable protocol 을 구현하지 않은Object 는 iterable 이 아닙니다.그러므로, 객체의 프로퍼티를 반복하기 위해for…of 를 사용하면 안됩니다.
js
var obj = { France: "Paris", England: "London" };for (let p of obj) { // TypeError: obj is not iterable // …}객체의 모든 항목 또는 프로퍼티를 반복하려면 대신Object.keys 또는Object.entries 를 사용해야 합니다.
js
var obj = { France: "Paris", England: "London" };// 모든 프로퍼티 이름을 iterate: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);이 유즈 케이스에 대한 다른 옵션은Map 을 사용하는 것입니다.
js
var map = new Map();map.set("France", "Paris");map.set("England", "London");// 모든 프로퍼티 이름 iteratefor (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);Generator iterating
Generators 는 iterable 객체를 생성하기 위해 호출하는 함수입니다.
js
function* generate(a, b) { yield a; yield b;}for (let x of generate) // TypeError: generate is not iterable console.log(x);generator 가 호출되지 않으면, generator 에 해당하는Function 객체를 호출할수는 있지만 interable 하지는 않습니다. generator 호출은 generator 실행동안 yield 된 모든 값을 iterate 하는 iterable 객체를 생성합니다.
js
function* generate(a, b) { yield a; yield b;}for (let x of generate(1, 2)) console.log(x);