Firefox: for-of and objects

# Axel Rauschmayer (13 years ago)

Sorry, slightly off-topic: Does Firefox already have @iter.items (etc.)? Or is there something similar one could use to iterate over [key, value] pairs for objects, via for-of?

Thanks!

Axel

# Marek Stępień (13 years ago)

On Thu, Oct 25, 2012 at 1:24 PM, Axel Rauschmayer <axel at rauschma.de> wrote:

Sorry, slightly off-topic: Does Firefox already have @iter.items (etc.)? Or is there something similar one could use to iterate over [key, value] pairs for objects, via for-of?

You can use Iterator, e.g.:

for ([key, value] of Iterator({a:1, b:2, c:3})) { alert(key + "=>" + value); }

# Jason Orendorff (13 years ago)

On Thu, Oct 25, 2012 at 6:24 AM, Axel Rauschmayer <axel at rauschma.de> wrote:

Sorry, slightly off-topic: Does Firefox already have @iter.items (etc.)? Or is there something similar one could use to iterate over [key, value] pairs for objects, via for-of?

Unfortunately Iterator does do that, but I tend to think you'd be better off using Object.keys. Of course you can also write your own:

function items(obj) { return [[k, obj[k]] for (k of Object.keys(obj))]; }

# Erik Arvidsson (13 years ago)

On Thu, Oct 25, 2012 at 11:50 AM, Jason Orendorff <jason.orendorff at gmail.com> wrote:

On Thu, Oct 25, 2012 at 6:24 AM, Axel Rauschmayer <axel at rauschma.de> wrote:

Sorry, slightly off-topic: Does Firefox already have @iter.items (etc.)? Or is there something similar one could use to iterate over [key, value] pairs for objects, via for-of?

Unfortunately Iterator does do that, but I tend to think you'd be better off using Object.keys. Of course you can also write your own:

function items(obj) { return [[k, obj[k]] for (k of Object.keys(obj))]; }

Don't you mean

function items(obj) { return ([k, obj[k]] for (k of Object.keys(obj))); }

Generator comprehension instead of array comprehension... items is suppused to return an iterable and in this case we don't need the Array.