ES6 iteration over object values

C. Scott Ananian ecmascript at cscott.net
Sat Mar 15 22:22:18 PDT 2014


On Sat, Mar 15, 2014 at 6:19 PM, David Bruant <bruant.d at gmail.com> wrote:
> Le 15/03/2014 22:51, C. Scott Ananian a écrit :
> It would be nicer to add an Object.entries() method that would return that
> iterator.
>
> Object.prototype.entries or Object.entries(obj)?

`Object.entries(obj)` and `Object.values(obj)` (as suggested by Jason).

The linked resolution for `Dict`
(https://github.com/rwaldron/tc39-notes/blob/master/es6/2012-11/nov-29.md#conclusionresolution-5)
was less than clear, but generic `Dict.keys(obj)`,
`Dict.entries(obj)`, and `Dict.values(obj)` which can take arbitrary
objects (not just objects with no prototype) would be an okay
fallback.  It leaves `Object.keys` an orphan, alas.
`Dict.prototype.entries.call(obj)` would be too long to be useful (but
I don't think that was being proposed).

```js
for (let [key, value] of Dict.entries(myObj)) {
   // do something with key|value
}
```

That's not too bad.  Shorter than `Object.entries`, even.  (But I
personally don't see the point of `new Dict()` instead of
`Object.create(null)`, and prefer the `Object.*` names.)
 --scott


More information about the es-discuss mailing list