2

On babel-node REPL I get

TypeError: undefined is not a function

when trying to iterate using a For..Of on an object. I don't get why a for..in would work, but a for..of won't. Is it only for Arrays?

const config = { base: 'iei', url: 'www.' }

for (let i of config) { console.log(i); }
cuadraman
  • 14,964
  • 7
  • 27
  • 32

1 Answers1

3

No, for of is for iterables. Not all objects are iterable. You can create a custom iterator for your object, though:

Object.values = function* (o) {
    for (let k of Object.keys(o))
        yield o[k];
};

for (let i of Object.values(config)) console.log(i); // 'iei', 'www.'
Bergi
  • 630,263
  • 148
  • 957
  • 1,375