Object iterator in JavaScript

Let's create an iterator that can be used to iterate over an object. First, let's make a generator that takes an object as a parameter and iterates over it:

function *func(obj) { for (let key in obj) { yield obj[key]; } }

Now let's create an iterator:

let iter = func({a: 1, b: 2, c: 3});

And loop through this iterator:

for (let elem of iter) { console.log(elem); // 1, 2, 3 }

Make an iterator that will iterate over an object and return an array with each call, the zero element of which will contain the key, and the first element will contain the value of the object element. An example:

let iter = func({a: 1, b: 2, c: 3}); for (let elem of iter) { console.log(elem); // ['a', 1], ['b', 2], ['c', 3] }
enru