⊗jsSpItOb 66 of 294 menu

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] }
English
AfrikaansAzərbaycanБългарскиবাংলাБеларускаяČeštinaDanskDeutschΕλληνικάEspañolEestiSuomiFrançaisहिन्दीMagyarՀայերենIndonesiaItaliano日本語ქართულიҚазақ한국어КыргызчаLietuviųLatviešuМакедонскиMelayuမြန်မာNederlandsNorskPolskiPortuguêsRomânăРусскийසිංහලSlovenčinaSlovenščinaShqipСрпскиSrpskiSvenskaKiswahiliТоҷикӣไทยTürkmenTürkçeЎзбекOʻzbekTiếng Việt
We use cookies for website operation, analytics, and personalization. Data processing is carried out in accordance with the Privacy Policy.
accept all customize decline