Providing object iterators beyond just Object.keys()

# Gijs Kruitbosch (8 years ago)


When writing JS loops over objects, given the environment I'm in, I find myself using the gecko/spidermonkey-only "Iterator()" construct a lot for loops or array comprehensions:

for (let [key, val] of Iterator(plainJSObject)) { // do stuff with both key and val }

It seems the plan is to remove "Iterator()" (cf. ).

Are there plans to add things like Object.values() (to produce Object.keys(obj).map(k => obj[k]) ) or Object.iterator (Object.keys(obj).map(k => [k, obj[k]])) so as to enable such usecases to

be written easily in ES7 or later? Obviously I can create my own iterator, but it starts to get tedious if I need to do this in every file where I need to iterate over an object's own (enumerable) properties...

(using Maps here is difficult because writing hardcoded nested structures of Maps is not nearly as neat as doing the same with Objects)


# Domenic Denicola (8 years ago)
# Gijs Kruitbosch (8 years ago)

Perhaps surprisingly, I had actually asked around and looked through recent threads. Apologies for not having found the previous discussions from a year ago. It seems that after the call for a strawman in the meeting notes from April 2014, there was silence? asks the same. I don't really know whether it's more useful to continue replying here or pull up that thread (is one month "old" by es-discuss standards?). Either way, it would be useful to know what the current status is, which none of the posts/notes that I saw really clarify.

# Rick Waldron (8 years ago)

Silence because it wasn't a priority, relative to finishing ES6. It's not forgotten and still on track for ES7 development.

# Michael Ficarra (8 years ago)

Has anyone drafted a proposal for this? Is anyone assigned as champion yet?

# Rick Waldron (8 years ago)

No draft written, but I am the current champion. If you'd like to get something started, we can co-champion :)

# Emanuel Allen (8 years ago)

You can simply use the Symbol.iterator property to configure how you want the property and value of the object to be iterated. This will allow you to use any from of iteration; e.g. For loop while loop forEach if you do the generic way I'm thinking .

If this help. Post me some link to resources that you have found on your journey as a js dev.