-
-
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
_.transform Applies a function to perform transformation on the object #1112
Conversation
Very interesting. This is only useful in the middle of a |
@jashkenas While that is true, I like to think of it as playing friendly with other libraries. :) Also, before this, the only way to apply your own function on the wrapped object while still keeping it under wrap is through |
But that would mask the other map, and they do have the same signature. On Jun 20, 2013, at 6:59 AM, "Mario T. Lanza" [email protected] wrote:
|
I think the name |
@mlanza I'm liking |
The reason // the number is passed thru `Object` and converted to an object
_(2).chain().passthru(Object).isObject().value(); // true
// the object is passed thru `Object` and returned unmodified
_({a: 1}).chain().passthru(Object).isObject().value(); // true |
Neat. I don't think it's quite useful enough to merit the complexity of explaining how to use it. Most of the time, to transform, you'll want to |
For anyone that's still looking for this function, lodash now has it. |
The goal is to supply a function to work with the object itself. Most underscore functions work with collections and treats objects as only associative array. #1068 may be related, but that pull request still work the object as an associative array.
The main use case is during chaining that you'll want to apply a certain function to the wrapped object.
Alternative solution without
_.transform
may be ugly because it disrupts the structure flow.Naming-wise, it may be more appropriate to call it
apply
. But since the_
object is itself a function and we don't want to overrideFunction.prototype.apply
, I came up with thetransform
alternative.Alternatively, the functionality may be merged with either
_.chain().value(function)
or_.tap(function)
by making the chain returns tap's function return value if it does return something. Something like this