[Proposal] Array.prototype.includes should evaluate object.toString and/or object.valueOf when determining a match

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

[Proposal] Array.prototype.includes should evaluate object.toString and/or object.valueOf when determining a match

Mike Simon
[Proposal] Array.prototype.includes should evaluate object.toString and/or object.valueOf when determining a match

The problem:  

If I have a list of objects that contain a toString() prototype that returns a value from the object, Array.prototype.includes does not return true if the value/string returned from the object is a match for the parameter value.

Test code:

var thingy = function(name) {this.name=name;};
thingy.prototype.valueOf = function(){return this.name};
thingy.prototype.toString = function(){return this.name};
var items = [new thingy('abc'),new thingy('def')];
var inc = items.includes('abc');
alert(items[0]); // alerts abc
alert(inc); // returns false

While it’s possible to create this as a list of strings using map, this would seem to conserve memory, as well as reduce the complexity of the code to perform this operation. 


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

Re: [Proposal] Array.prototype.includes should evaluate object.toString and/or object.valueOf when determining a match

Peter Jaszkowiak
If you want to check properties or methods of an array of objects, you can use `Array.prototype.some`.

On Wed, Jun 20, 2018, 12:27 Mike Simon <[hidden email]> wrote:
[Proposal] Array.prototype.includes should evaluate object.toString and/or object.valueOf when determining a match

The problem:  

If I have a list of objects that contain a toString() prototype that returns a value from the object, Array.prototype.includes does not return true if the value/string returned from the object is a match for the parameter value.

Test code:

var thingy = function(name) {this.name=name;};
thingy.prototype.valueOf = function(){return this.name};
thingy.prototype.toString = function(){return this.name};
var items = [new thingy('abc'),new thingy('def')];
var inc = items.includes('abc');
alert(items[0]); // alerts abc
alert(inc); // returns false

While it’s possible to create this as a list of strings using map, this would seem to conserve memory, as well as reduce the complexity of the code to perform this operation. 

_______________________________________________
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: [Proposal] Array.prototype.includes should evaluate object.toString and/or object.valueOf when determining a match

Andrea Giammarchi-2
In reply to this post by Mike Simon
It looks like you want included to use `==` for comparison, opening a whole new world of issues.

You have `some` and your code to do that with ease, same goes for `find` or `findIndex`, very appropriate for this use case.

```js
items.some(o => o == 'abc');
```

Regards

On Wed, Jun 20, 2018 at 8:27 PM, Mike Simon <[hidden email]> wrote:
[Proposal] Array.prototype.includes should evaluate object.toString and/or object.valueOf when determining a match

The problem:  

If I have a list of objects that contain a toString() prototype that returns a value from the object, Array.prototype.includes does not return true if the value/string returned from the object is a match for the parameter value.

Test code:

var thingy = function(name) {this.name=name;};
thingy.prototype.valueOf = function(){return this.name};
thingy.prototype.toString = function(){return this.name};
var items = [new thingy('abc'),new thingy('def')];
var inc = items.includes('abc');
alert(items[0]); // alerts abc
alert(inc); // returns false

While it’s possible to create this as a list of strings using map, this would seem to conserve memory, as well as reduce the complexity of the code to perform this operation. 


_______________________________________________
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: [Proposal] Array.prototype.includes should evaluate object.toString and/or object.valueOf when determining a match

Mike Simon
True, there are many other ways to make this work. You could even use filter(function(f){f.name==‘abc’;}).length > 0 .

The focus of this proposal is that the purpose of .valueOf and .toString are to allow for objects to return a default value without requiring the matching method to know about the contents or property names of any of next. 

Array.prototype.some, and any method mentioned above require a that a content aware function be passed in to facilitate the match.  This proposal mitigates that, and allows Array.prototype.includes to evaluate the object as the object design was intended. 

On Jun 20,  2018, at 11:33 AM, Andrea  <[hidden email]> wrote:

It looks like you want included to use `==` for comparison, opening a whole new world of issues.

You have `some` and your code to do that with ease, same goes for `find` or `findIndex`, very appropriate for this use case.

```js
items.some(o => o == 'abc');
```

Regards

On Wed, Jun 20, 2018 at 8:27 PM, Mike Simon <[hidden email]> wrote:
[Proposal] Array.prototype.includes should evaluate object.toString and/or object.valueOf when determining a match

The problem:  

If I have a list of objects that contain a toString() prototype that returns a value from the object, Array.prototype.includes does not return true if the value/string returned from the object is a match for the parameter value.

Test code:

var thingy = function(name) {this.name=name;};
thingy.prototype.valueOf = function(){return this.name};
thingy.prototype.toString = function(){return this.name};
var items = [new thingy('abc'),new thingy('def')];
var inc = items.includes('abc');
alert(items[0]); // alerts abc
alert(inc); // returns false

While it’s possible to create this as a list of strings using map, this would seem to conserve memory, as well as reduce the complexity of the code to perform this operation. 


_______________________________________________
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: [Proposal] Array.prototype.includes should evaluate object.toString and/or object.valueOf when determining a match

Andrea Giammarchi-2
You could even use filter(function(f){f.name==‘abc’;}).length > 0

not really, that's a long way to find what you are looking for, `some` stops ASAP instead.

The focus of this proposal is that the purpose of .valueOf and .toString

I think those have valid use cases and changing a method already shipped long time ago to make use of those methods is surely not one of them.

This will never happen, you might have better luck proposing a new method (yet I have the feeling it won't happen neither because it's too easy to do on user land).

Regards


On Wed, Jun 20, 2018 at 8:54 PM, Mike Simon <[hidden email]> wrote:
True, there are many other ways to make this work. You could even use filter(function(f){f.name==‘abc’;}).length > 0 .

The focus of this proposal is that the purpose of .valueOf and .toString are to allow for objects to return a default value without requiring the matching method to know about the contents or property names of any of next. 

Array.prototype.some, and any method mentioned above require a that a content aware function be passed in to facilitate the match.  This proposal mitigates that, and allows Array.prototype.includes to evaluate the object as the object design was intended. 

On Jun 20,  2018, at 11:33 AM, Andrea  <[hidden email]> wrote:

It looks like you want included to use `==` for comparison, opening a whole new world of issues.

You have `some` and your code to do that with ease, same goes for `find` or `findIndex`, very appropriate for this use case.

```js
items.some(o => o == 'abc');
```

Regards

On Wed, Jun 20, 2018 at 8:27 PM, Mike Simon <[hidden email]> wrote:
[Proposal] Array.prototype.includes should evaluate object.toString and/or object.valueOf when determining a match

The problem:  

If I have a list of objects that contain a toString() prototype that returns a value from the object, Array.prototype.includes does not return true if the value/string returned from the object is a match for the parameter value.

Test code:

var thingy = function(name) {this.name=name;};
thingy.prototype.valueOf = function(){return this.name};
thingy.prototype.toString = function(){return this.name};
var items = [new thingy('abc'),new thingy('def')];
var inc = items.includes('abc');
alert(items[0]); // alerts abc
alert(inc); // returns false

While it’s possible to create this as a list of strings using map, this would seem to conserve memory, as well as reduce the complexity of the code to perform this operation. 


_______________________________________________
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