Object.values and/or Object.forEach ?

Dmitry Soshnikov dmitry.soshnikov at gmail.com
Sat Jun 8 21:23:55 PDT 2013


I might be missing something, but how for-of help solving parametrized enumeration (that is, specifying an action applied on a value/key in a functional style)?

I see clear use-case of Object.values(...) in addition to Object.keys(...), and it has nothing to do with for/of enumeration.

Why refuse a convenient library method and force users to 1) let values = []; 2) for (v of values(o)) { values.push(v); }, if it can be just let values = Object.values(o);?

Why refuse a _parametrized_ functional iteration such as Object.forEach(o, parametrizedAction) and force users to write different static for-of loops with own loop body per each condition, if this condition can be passed as a function?

P.S.: for consistency with [].forEach, it probably would make sense having {}.forEach, but it's O.prototype pollution.

Dmitry

On Jun 7, 2013, at 10:18 AM, Dean Landolt wrote:

> The for/of iterators solve this nicely. This is definitely something that comes up a lot though, and this seems like a very handy cowpath to pave. If it were spec'd I'd suggest the naming and argument values should align with the for/of variants.
> 
> 
> On Fri, Jun 7, 2013 at 12:57 PM, Andrea Giammarchi <andrea.giammarchi at gmail.com> wrote:
> it comes out from time to time devs would like to have `Object.values()` as equivalent of `Object.keys()` except the returned array would contain values.
> 
> Better than repeated
> `Object.keys(obj).map(function(p){return this[k];}, obj);`
> all over
> 
> but probably less useful than a more generic `Object.forEach(obj, callback, thisValue)` where `callback` would receive `(value, key, originalObject)` and `thisValue` as context.
> 
> This is consistent with `Array#forEach` and could simplify `for/in` loops passing over own enumerable properties only, as keys would do.
> 
> Thoughts ?
> 
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss
> 
> 
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20130608/82458906/attachment.html>


More information about the es-discuss mailing list