for...of loop produces "Uncaught TypeError: myObject[Symbol.iterator] is not a function" error

See original GitHub issue

The transpiler currently seems to be unable to handle for…of loops. Using them causes Uncaught TypeError: myObject[Symbol.iterator] is not a function

For example, the following code:

    var myObject = {
        "key1": "value1",
        "key2": "value2",
        "key3": "value3",
        "fn1": function(){ 
            console.log("fn1 ran!"); 
        },
        "fn2": function(param){
            console.log(param); 
        },
        "number1": 4
    }

    for (let item of myObject){
        console.log(item);
    }

…which gets converted into:

    var myObject = {
        "key1": "value1",
        "key2": "value2",
        "key3": "value3",
        "fn1": function fn1() {
            console.log("fn1 ran!");
        },
        "fn2": function fn2(param) {
            console.log(param);
        },
        "number1": 4
    };

    var _iteratorNormalCompletion = true;
    var _didIteratorError = false;
    var _iteratorError = undefined;

    try {
        for (var _iterator = myObject[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
            var item = _step.value;
            console.log(item);
        }
    } catch (err) {
        _didIteratorError = true;
        _iteratorError = err;
    } finally {
        try {
            if (!_iteratorNormalCompletion && _iterator["return"]) {
                _iterator["return"]();
            }
        } finally {
            if (_didIteratorError) {
                throw _iteratorError;
            }
        }
    }

…produces the following error when run:

 'Uncaught TypeError: someObj[Symbol.iterator] is not a function'
 utilMainModule  @                               app.js:28208
 (anonymous function)  @                   app.js:28316
 _curFld    @                                         app.js:28317
 __webpack_require__   @                   app.js:30
 main    @                                        app.js:523
 navdrawerModule   @                          app.js:26701
 (anonymous function)   @                  app.js:26873
 moduleNm    @                                    app.js:26875
 __webpack_require__    @                  app.js:30
                   [.............(more items)..........]
(anonymous function) @                     app.js:10
webpackUniversalModuleDefinition @ app.js:9
(anonymous function) @                     app.js:53

This occurs in both Chrome and Firefox. I’ve tried it in several different places in my codebase, with the same result every time.

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Reactions:7
  • Comments:16 (6 by maintainers)

github_iconTop GitHub Comments

180reactions
zloirockcommented, Jul 9, 2015

@andfaulkner stop… I didn’t saw your code. Plain objects are not iterable. You can use something like

for (let [key, value] of Object.entries(myObject)){
  console.log(key, value);
}

or add custom iterator to your object.

7reactions
Coutocommented, Jul 8, 2015

Have you imported the polyfill?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Symbol.iterator is not a function [duplicate] - Stack Overflow
When i run this code I see error. Uncaught TypeError: ele[Symbol.iterator] is not a function. How to fix this problem ?
Read more >
for...of - JavaScript | MDN - MDN Web Docs
Each operation of the loop on a value is called an iteration, and the loop is said to iterate over the iterable. Each...
Read more >
JavaScript: Complete guide to For…of Loop - Bits and Pieces
You see it always returns a Promise, the Promise has a resolve function that returns a value at each iteration. To iterate through...
Read more >
17. The for-of loop - Exploring JS
for-of is a new loop in ES6 that replaces both for-in and forEach() and ... you want to iterate over plain objects (see...
Read more >
Make your JS objects iterable - Rajat Explains
We know when we want to iterate over an object, we can iterate over ... loop, we get this It throws an error...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found