-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Underscore could use a _.propertyResult #2286
Comments
Lodash added |
…ctions on an Object. Closes jashkenas#2286: Underscore could use a _.propertyResult.
…ctions on an Object. Closes jashkenas#2286: Underscore could use a _.propertyResult.
I wonder if this feature is still relvant, and is worthy of development in Javascript's current eco-system. Since this suggestion was originally published, arrow function became a popular and wide spread feature in the Javascript ecosystem. At the time @machineghost had to write: _(objects).any(function(x) { return x.isNew; }); But today we can simply write: _(objects).any( (x)=>x.isNew ); @machineghost wanted to be able to write: _(objects).any(_('isNew').propertyResult()); But today we can write: _(objects).any( (x)=>x.isNew() ); Personally I find the arrow function version much more readable. |
@carpben I think you are right that with modern syntax, So I agree that it doesn't seem right to add this to Underscore at this time, but I would still welcome it as a contribution to Underscore-contrib. Contrib could be regarded as a proofing ground for suggested new features for Underscore. If Contrib offers I should also mention that we currently still support some environments that don't have arrow functions and that not everyone may want or be able to use a transpiler. That's not a reason to add the function by itself, but still something to keep in mind. |
The new
_.property
is really neat, and I love using it, but there's one problem: it only returns static values.For instance, let's say I want to find out if any object in an array contains has an
isNew
property; I can do the following:That's a lot better than before
property
, when I had to do:But what if instead of random objects I have a bunch of
Backbone.Model
objects. Those don't have anisNew
property, they have anisNew
method. So:won't work; instead I'm back to the old style:
or (if I combine
partial
andresult
):It would be really great if there was a method that was just like
_.property
, except that if it would return a function, it returns the result of executing that function instead (like_.result
). In other words, if I could do ...Since it's a fairly common practice to have
is*
methods on objects, and also common practice to want to use Underscore methods likeany
orfilter
with those methods, I really think this would be a useful addition to the library.The text was updated successfully, but these errors were encountered: