Object.values and/or Object.forEach ?

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


On Jun 8, 2013, at 9:31 PM, Yehuda Katz wrote:

> 
> 
> Yehuda Katz
> (ph) 718.877.1325
> 
> 
> On Sat, Jun 8, 2013 at 9:23 PM, Dmitry Soshnikov <dmitry.soshnikov at gmail.com> wrote:
> 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);?
> 
> I'm confused. Your example shows (correctly) that a `values` method exists that does what you want. What more are you asking for?
>  

I'm confused either (please correct me if I'm mistaken), since values(...) generator method doesn't do what I want, it returns a _gen_, but not an array of values corresponding to own enumerable properties (returned by Object.keys(...) with the same enumeration order).

> 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?
> 
> I am personally in favor of a collections module that works across any iterable. I don't think we have anything on tap for ES6, but it would be trivial to write in pure JS in the ES6 timeframe and I don't see any reason it couldn't be folded in to ES7 once it gets some real-world usage.
> 

Yeah, a generic parametrized enumeration in the function style would be nice to have. As for a clear practical use-case, I don't see the reason to wait until ES7 if this may happen now:

process(o, action) {
  if (action == 'init') {
    for (let v of values(o)) {
      this.initValue(v);
    }
  } else if (action == 'dump') {
    for (let v of values(o)) {
      this.dumpData(v);
    }
  } else if (/* whaever */) {
    // how many else parametrized actions
    // should I apply before we have a parametrized
    // enumerator?
  }
}

Which reduces basically to:

Object.forEach(o, this.init);
Object.forEach(o, this.dump);
// etc.

And of course everything can be implemented on a user-level, but why not to provide it out of the box?

Dmitry

> 
> 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
> 
> 
> _______________________________________________
> 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/fca367fe/attachment.html>


More information about the es-discuss mailing list