DEV Community

Discussion on: Looping through objects in JavaScript

Collapse
 
kepta profile image
Kushan Joshi • Edited

Zell Liew, just wanted to add my 2cents to this well written article.

I feel if one realises that a particular object would require a lot of iteration then maybe they need to start thinking about ditching the object in favour of data structures which have a first party support for iteration.

With ES6 we have a new fancy data structure called Map and it is a perfect fit for those cases where you are overloading an object with thousands of properties and also want to iterate through them.

A simple example of Map.

var map = new Map();

map.set('random', 34);
map.set('abc', 42);

for(const val of map.values()) {
   console.log(val);
}

MDN Docs for Map

Collapse
 
zellwk profile image
Zell Liew 🤗

Thanks for adding on!