Additional methods for Objects (like Arrays)

classic Classic list List threaded Threaded
14 messages Options
Reply | Threaded
Open this post in threaded view
|

Additional methods for Objects (like Arrays)

Kaustubh Karkare
I have recently come to feel the need for Object.map, which is like Array.map,
except that it receive keys instead of indices.

Object.prototype.map = function(mapFn, context) {
  return Object.keys(this)
    .reduce(function(result, key) {
      result[key] = mapFn.call(context, this[key], key, this);
      return result;
    }, {});
};

Without this, I frequently do the exact same thing as the above manually,
which leads to unnecessary code duplication.

Given that, it might make sense to match other methods from Array.prototype

Object.map
Object.filter
Object.every
Object.some
Object.reduce
Object.find
Object.findKey // like Array.findIndex

Note that wherever applicable, the ordering is non-deterministic.


_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Additional methods for Objects (like Arrays)

Dmitry Soshnikov
Yeah, some languages have this functionality available for maps, e.g. Erlang http://erlang.org/doc/man/maps.html#map-2. Might be a good addition, although considering `Map` instead of `Object` is worth as well. (although, when we were discussing of adding `Map.prototype.map/filter`, it was decided that it's better to add them on iterators).

Dmitry

On Fri, Jan 29, 2016 at 3:07 PM, Kaustubh Karkare <[hidden email]> wrote:
I have recently come to feel the need for Object.map, which is like Array.map,
except that it receive keys instead of indices.

Object.prototype.map = function(mapFn, context) {
  return Object.keys(this)
    .reduce(function(result, key) {
      result[key] = mapFn.call(context, this[key], key, this);
      return result;
    }, {});
};

Without this, I frequently do the exact same thing as the above manually,
which leads to unnecessary code duplication.

Given that, it might make sense to match other methods from Array.prototype

Object.map
Object.filter
Object.every
Object.some
Object.reduce
Object.find
Object.findKey // like Array.findIndex

Note that wherever applicable, the ordering is non-deterministic.


_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss



_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Additional methods for Objects (like Arrays)

Dmitry Soshnikov
In reply to this post by Kaustubh Karkare


On Fri, Jan 29, 2016 at 3:07 PM, Kaustubh Karkare <[hidden email]> wrote:
I have recently come to feel the need for Object.map, which is like Array.map,
except that it receive keys instead of indices.

Object.prototype.map = function(mapFn, context) {
  return Object.keys(this)
    .reduce(function(result, key) {
      result[key] = mapFn.call(context, this[key], key, this);
      return result;
    }, {});
};


Also a potential issue of placing it on `Object.prototype` is that other things will inherit it, including unrelated (like strings, regexps, functions, etc). By this reason seems iterator methods `values`, `keys`, and `entries` weren't added in ES6 to `Object.prototype`.

Dmitry

 
Without this, I frequently do the exact same thing as the above manually,
which leads to unnecessary code duplication.

Given that, it might make sense to match other methods from Array.prototype

Object.map
Object.filter
Object.every
Object.some
Object.reduce
Object.find
Object.findKey // like Array.findIndex

Note that wherever applicable, the ordering is non-deterministic.


_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss



_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Additional methods for Objects (like Arrays)

Rick Waldron
In reply to this post by Kaustubh Karkare


On Fri, Jan 29, 2016 at 6:08 PM Kaustubh Karkare <[hidden email]> wrote:
I have recently come to feel the need for Object.map, which is like Array.map,
except that it receive keys instead of indices.

Object.prototype.map = function(mapFn, context) {
  return Object.keys(this)
    .reduce(function(result, key) {
      result[key] = mapFn.call(context, this[key], key, this);
      return result;
    }, {});
};

Without this, I frequently do the exact same thing as the above manually,
which leads to unnecessary code duplication.

Given that, it might make sense to match other methods from Array.prototype

Object.map
Object.filter
Object.every
Object.some
Object.reduce
Object.find
Object.findKey // like Array.findIndex


Are these necessary given the introduction of Object.values() and Object.entries()? https://github.com/tc39/proposal-object-values-entries

Rick

_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Re: Additional methods for Objects (like Arrays)

Alican Çubukçuoğlu
In reply to this post by Kaustubh Karkare
> Are these necessary given the introduction of Object.values() and Object.entries()?

`Object.entries()` works. `Object.p.map()` would be shorter to write, easier to read and more compatible with functions already written for `Array.p.map()`.

_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Additional methods for Objects (like Arrays)

Dmitry Soshnikov
In reply to this post by Rick Waldron


On Friday, January 29, 2016, Rick Waldron <[hidden email]> wrote:


On Fri, Jan 29, 2016 at 6:08 PM Kaustubh Karkare <<a href="javascript:_e(%7B%7D,&#39;cvml&#39;,&#39;kaustubh.karkare@gmail.com&#39;);" target="_blank">kaustubh.karkare@...> wrote:
I have recently come to feel the need for Object.map, which is like Array.map,
except that it receive keys instead of indices.

Object.prototype.map = function(mapFn, context) {
  return Object.keys(this)
    .reduce(function(result, key) {
      result[key] = mapFn.call(context, this[key], key, this);
      return result;
    }, {});
};

Without this, I frequently do the exact same thing as the above manually,
which leads to unnecessary code duplication.

Given that, it might make sense to match other methods from Array.prototype

Object.map
Object.filter
Object.every
Object.some
Object.reduce
Object.find
Object.findKey // like Array.findIndex


Are these necessary given the introduction of Object.values() and Object.entries()? https://github.com/tc39/proposal-object-values-entries


I think what Kaustubh is proposing is that the result is a transformed object/map, not an array. See the reduce method in his implementation.

Dmitry 

 
Rick

_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Additional methods for Objects (like Arrays)

段垚
In reply to this post by Kaustubh Karkare
在 2016/1/30 7:07, Kaustubh Karkare 写道:
I have recently come to feel the need for Object.map, which is like Array.map,
except that it receive keys instead of indices.

Object.prototype.map = function(mapFn, context) {
  return Object.keys(this)
    .reduce(function(result, key) {
      result[key] = mapFn.call(context, this[key], key, this);
      return result;
    }, {});
};

Without this, I frequently do the exact same thing as the above manually,
which leads to unnecessary code duplication.

Given that, it might make sense to match other methods from Array.prototype

Object.map
Object.filter
Object.every
Object.some
Object.reduce
Object.find
Object.findKey // like Array.findIndex

Note that wherever applicable, the ordering is non-deterministic.
I'd rather add them as static methods of Object, e.g.

    var result = Object.map(obj, function(value, key) { ... });

Because when I use an object as a map, I usually create it without prototype (Object.create(null)),
to avoid unintentional access of properties on Object.prototype.




_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss


_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

RE: Additional methods for Objects (like Arrays)

Gary Guo
In reply to this post by Kaustubh Karkare
Not all objects are used as maps, and when using with map we often use `Object.create(null)`, so adding to prototype is definitely a bad idea. As for whether we should make them static methods of Object, I personally don't think this is very useful. Using a for-in loop is enough in my opinion.


From: [hidden email]
Date: Fri, 29 Jan 2016 18:07:30 -0500
Subject: Additional methods for Objects (like Arrays)
To: [hidden email]

I have recently come to feel the need for Object.map, which is like Array.map,
except that it receive keys instead of indices.

Object.prototype.map = function(mapFn, context) {
  return Object.keys(this)
    .reduce(function(result, key) {
      result[key] = mapFn.call(context, this[key], key, this);
      return result;
    }, {});
};

Without this, I frequently do the exact same thing as the above manually,
which leads to unnecessary code duplication.

Given that, it might make sense to match other methods from Array.prototype

Object.map
Object.filter
Object.every
Object.some
Object.reduce
Object.find
Object.findKey // like Array.findIndex

Note that wherever applicable, the ordering is non-deterministic.


_______________________________________________ es-discuss mailing list [hidden email] https://mail.mozilla.org/listinfo/es-discuss

_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Additional methods for Objects (like Arrays)

Kaustubh Karkare
Not all objects are used as maps, and when using with map we often use `Object.create(null)`, so adding to prototype is definitely a bad idea.

- Much like we're forced to invoke the prototype directly like (in functions & NodeLists) `Array.prototype.slice.call(arguments)`, an argument could be made that one should need invoke `Object.prototype.map.call(objWithoutPrototype, mapFn)`.
- My main issue with static methods on the object is the lack of consistency with the array format. But given that we already have `Object.keys` and `Object.values`, there's already precedent for this (plus the argument about breaking legacy frameworks). Note that this methods should be on the `Map.prototype` at least (given `Map.prototype.keys/values` exist), since this argument doesn't apply in that case.

As for whether we should make them static methods of Object, I personally don't think this is very useful. Using a for-in loop is enough in my opinion.

For-in loops result in unnecessary verbosity, specially in codebases that optimize for readability. Comparing something like:

```
const normalizedGrey = colors
  .map(c => (c.red + c.green + c.blue) / 3)
  .filter(g => g < someThreshold)
  .map(g => (g - min) / (max - min));
```
vs

```
let normalizedGrey = {};
for (var color in colors) {
  let c = colors[color];
  let g = (c.red + c.green + c.blue) / 3;
  if (g < someThreshold) {
    normalizedGrey[color] = (g - min) / (max - min);
  }
}
```
The latter has more lines/bytes and is harder to read.

On Sat, Jan 30, 2016 at 11:02 AM, Gary Guo <[hidden email]> wrote:
Not all objects are used as maps, and when using with map we often use `Object.create(null)`, so adding to prototype is definitely a bad idea. As for whether we should make them static methods of Object, I personally don't think this is very useful. Using a for-in loop is enough in my opinion.


From: [hidden email]
Date: Fri, 29 Jan 2016 18:07:30 -0500
Subject: Additional methods for Objects (like Arrays)
To: [hidden email]


I have recently come to feel the need for Object.map, which is like Array.map,
except that it receive keys instead of indices.

Object.prototype.map = function(mapFn, context) {
  return Object.keys(this)
    .reduce(function(result, key) {
      result[key] = mapFn.call(context, this[key], key, this);
      return result;
    }, {});
};

Without this, I frequently do the exact same thing as the above manually,
which leads to unnecessary code duplication.

Given that, it might make sense to match other methods from Array.prototype

Object.map
Object.filter
Object.every
Object.some
Object.reduce
Object.find
Object.findKey // like Array.findIndex

Note that wherever applicable, the ordering is non-deterministic.


_______________________________________________ es-discuss mailing list [hidden email] https://mail.mozilla.org/listinfo/es-discuss


_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Additional methods for Objects (like Arrays)

Gilbert B Garza
The [this-bind operator](https://github.com/zenparsing/es-function-bind) would remove the need for officially adding methods to `Object.protoype`, wouldn't it? For example:

```
import { map, filter } from 'external-lib/objects'

const normalizedGrey = colors
  ::map(c => (c.red + c.green + c.blue) / 3)
  ::filter(g => g < someThreshold)
  ::map(g => (g - min) / (max - min));
```

On Sat, Jan 30, 2016 at 1:47 PM, Kaustubh Karkare <[hidden email]> wrote:
Not all objects are used as maps, and when using with map we often use `Object.create(null)`, so adding to prototype is definitely a bad idea.

- Much like we're forced to invoke the prototype directly like (in functions & NodeLists) `Array.prototype.slice.call(arguments)`, an argument could be made that one should need invoke `Object.prototype.map.call(objWithoutPrototype, mapFn)`.
- My main issue with static methods on the object is the lack of consistency with the array format. But given that we already have `Object.keys` and `Object.values`, there's already precedent for this (plus the argument about breaking legacy frameworks). Note that this methods should be on the `Map.prototype` at least (given `Map.prototype.keys/values` exist), since this argument doesn't apply in that case.

As for whether we should make them static methods of Object, I personally don't think this is very useful. Using a for-in loop is enough in my opinion.

For-in loops result in unnecessary verbosity, specially in codebases that optimize for readability. Comparing something like:

```
const normalizedGrey = colors
  .map(c => (c.red + c.green + c.blue) / 3)
  .filter(g => g < someThreshold)
  .map(g => (g - min) / (max - min));
```
vs

```
let normalizedGrey = {};
for (var color in colors) {
  let c = colors[color];
  let g = (c.red + c.green + c.blue) / 3;
  if (g < someThreshold) {
    normalizedGrey[color] = (g - min) / (max - min);
  }
}
```
The latter has more lines/bytes and is harder to read.

On Sat, Jan 30, 2016 at 11:02 AM, Gary Guo <[hidden email]> wrote:
Not all objects are used as maps, and when using with map we often use `Object.create(null)`, so adding to prototype is definitely a bad idea. As for whether we should make them static methods of Object, I personally don't think this is very useful. Using a for-in loop is enough in my opinion.


From: [hidden email]
Date: Fri, 29 Jan 2016 18:07:30 -0500
Subject: Additional methods for Objects (like Arrays)
To: [hidden email]


I have recently come to feel the need for Object.map, which is like Array.map,
except that it receive keys instead of indices.

Object.prototype.map = function(mapFn, context) {
  return Object.keys(this)
    .reduce(function(result, key) {
      result[key] = mapFn.call(context, this[key], key, this);
      return result;
    }, {});
};

Without this, I frequently do the exact same thing as the above manually,
which leads to unnecessary code duplication.

Given that, it might make sense to match other methods from Array.prototype

Object.map
Object.filter
Object.every
Object.some
Object.reduce
Object.find
Object.findKey // like Array.findIndex

Note that wherever applicable, the ordering is non-deterministic.


_______________________________________________ es-discuss mailing list [hidden email] https://mail.mozilla.org/listinfo/es-discuss


_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss



_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Additional methods for Objects (like Arrays)

Simon Blackwell
In reply to this post by Rick Waldron
Although Rick makes a good point that Object.values and Object.entries make creating functionality similar to that describe by Kaustubh, they are no exactly direct. We have added the requested functions to V2.0 of JavaScript Object Extensions at https://github.com/anywhichway/joex.

On Fri, Jan 29, 2016 at 7:44 PM, Rick Waldron <[hidden email]> wrote:


On Fri, Jan 29, 2016 at 6:08 PM Kaustubh Karkare <[hidden email]> wrote:
I have recently come to feel the need for Object.map, which is like Array.map,
except that it receive keys instead of indices.

Object.prototype.map = function(mapFn, context) {
  return Object.keys(this)
    .reduce(function(result, key) {
      result[key] = mapFn.call(context, this[key], key, this);
      return result;
    }, {});
};

Without this, I frequently do the exact same thing as the above manually,
which leads to unnecessary code duplication.

Given that, it might make sense to match other methods from Array.prototype

Object.map
Object.filter
Object.every
Object.some
Object.reduce
Object.find
Object.findKey // like Array.findIndex


Are these necessary given the introduction of Object.values() and Object.entries()? https://github.com/tc39/proposal-object-values-entries

Rick

_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss



_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Additional methods for Objects (like Arrays)

Isiah Meadows-2

I think that these methods should exist in iterator prototypes, with the native iterators subclassing something like Iterator. Object.entries and Object.values should return iterators that inherit these methods. Such wrapper libraries are already coming out, and making existing arrays lazily iterate already exists in `Symbol.iterator`.


On Tue, Feb 2, 2016, 12:12 Simon Blackwell <[hidden email]> wrote:
Although Rick makes a good point that Object.values and Object.entries make creating functionality similar to that describe by Kaustubh, they are no exactly direct. We have added the requested functions to V2.0 of JavaScript Object Extensions at https://github.com/anywhichway/joex.


On Fri, Jan 29, 2016 at 7:44 PM, Rick Waldron <[hidden email]> wrote:


On Fri, Jan 29, 2016 at 6:08 PM Kaustubh Karkare <[hidden email]> wrote:
I have recently come to feel the need for Object.map, which is like Array.map,
except that it receive keys instead of indices.

Object.prototype.map = function(mapFn, context) {
  return Object.keys(this)
    .reduce(function(result, key) {
      result[key] = mapFn.call(context, this[key], key, this);
      return result;
    }, {});
};

Without this, I frequently do the exact same thing as the above manually,
which leads to unnecessary code duplication.

Given that, it might make sense to match other methods from Array.prototype

Object.map
Object.filter
Object.every
Object.some
Object.reduce
Object.find
Object.findKey // like Array.findIndex


Are these necessary given the introduction of Object.values() and Object.entries()? https://github.com/tc39/proposal-object-values-entries

Rick

_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss


_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss

_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Additional methods for Objects (like Arrays)

Jordan Harband
Object.entries and Object.values must remain consistent with Object.keys and will return arrays - luckily those are iterable too.

On Friday, February 5, 2016, Isiah Meadows <[hidden email]> wrote:

I think that these methods should exist in iterator prototypes, with the native iterators subclassing something like Iterator. Object.entries and Object.values should return iterators that inherit these methods. Such wrapper libraries are already coming out, and making existing arrays lazily iterate already exists in `Symbol.iterator`.


On Tue, Feb 2, 2016, 12:12 Simon Blackwell <<a href="javascript:_e(%7B%7D,&#39;cvml&#39;,&#39;syblackwell@anywhichway.com&#39;);" target="_blank">syblackwell@...> wrote:
Although Rick makes a good point that Object.values and Object.entries make creating functionality similar to that describe by Kaustubh, they are no exactly direct. We have added the requested functions to V2.0 of JavaScript Object Extensions at https://github.com/anywhichway/joex.


On Fri, Jan 29, 2016 at 7:44 PM, Rick Waldron <<a href="javascript:_e(%7B%7D,&#39;cvml&#39;,&#39;waldron.rick@gmail.com&#39;);" target="_blank">waldron.rick@...> wrote:


On Fri, Jan 29, 2016 at 6:08 PM Kaustubh Karkare <<a href="javascript:_e(%7B%7D,&#39;cvml&#39;,&#39;kaustubh.karkare@gmail.com&#39;);" target="_blank">kaustubh.karkare@...> wrote:
I have recently come to feel the need for Object.map, which is like Array.map,
except that it receive keys instead of indices.

Object.prototype.map = function(mapFn, context) {
  return Object.keys(this)
    .reduce(function(result, key) {
      result[key] = mapFn.call(context, this[key], key, this);
      return result;
    }, {});
};

Without this, I frequently do the exact same thing as the above manually,
which leads to unnecessary code duplication.

Given that, it might make sense to match other methods from Array.prototype

Object.map
Object.filter
Object.every
Object.some
Object.reduce
Object.find
Object.findKey // like Array.findIndex


Are these necessary given the introduction of Object.values() and Object.entries()? https://github.com/tc39/proposal-object-values-entries

Rick

_______________________________________________
es-discuss mailing list
<a href="javascript:_e(%7B%7D,&#39;cvml&#39;,&#39;es-discuss@mozilla.org&#39;);" target="_blank">es-discuss@...
https://mail.mozilla.org/listinfo/es-discuss


_______________________________________________
es-discuss mailing list
<a href="javascript:_e(%7B%7D,&#39;cvml&#39;,&#39;es-discuss@mozilla.org&#39;);" target="_blank">es-discuss@...
https://mail.mozilla.org/listinfo/es-discuss

_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Additional methods for Objects (like Arrays)

Isiah Meadows-2

Ok. Never mind. I forgot about that part (I hadn't looked it up lately...) :)


On Fri, Feb 5, 2016, 14:26 Jordan Harband <[hidden email]> wrote:
Object.entries and Object.values must remain consistent with Object.keys and will return arrays - luckily those are iterable too.

On Friday, February 5, 2016, Isiah Meadows <[hidden email]> wrote:

I think that these methods should exist in iterator prototypes, with the native iterators subclassing something like Iterator. Object.entries and Object.values should return iterators that inherit these methods. Such wrapper libraries are already coming out, and making existing arrays lazily iterate already exists in `Symbol.iterator`.


On Tue, Feb 2, 2016, 12:12 Simon Blackwell <[hidden email]> wrote:
Although Rick makes a good point that Object.values and Object.entries make creating functionality similar to that describe by Kaustubh, they are no exactly direct. We have added the requested functions to V2.0 of JavaScript Object Extensions at https://github.com/anywhichway/joex.


On Fri, Jan 29, 2016 at 7:44 PM, Rick Waldron <[hidden email]> wrote:


On Fri, Jan 29, 2016 at 6:08 PM Kaustubh Karkare <[hidden email]> wrote:
I have recently come to feel the need for Object.map, which is like Array.map,
except that it receive keys instead of indices.

Object.prototype.map = function(mapFn, context) {
  return Object.keys(this)
    .reduce(function(result, key) {
      result[key] = mapFn.call(context, this[key], key, this);
      return result;
    }, {});
};

Without this, I frequently do the exact same thing as the above manually,
which leads to unnecessary code duplication.

Given that, it might make sense to match other methods from Array.prototype

Object.map
Object.filter
Object.every
Object.some
Object.reduce
Object.find
Object.findKey // like Array.findIndex


Are these necessary given the introduction of Object.values() and Object.entries()? https://github.com/tc39/proposal-object-values-entries

Rick

_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss


_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss

_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss