- Notifications
You must be signed in to change notification settings - Fork7.1k
Does lodash have functions for constructing (i.e. building brick by brick) an object?#5865
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
-
Hey, I've encountered this need a lot, and lately, I've been toying with the idea of opening a PR about it. But before I do, I want to make sure there really isn't a way of doing what I need using pure Lodash. Case 1Functionally add/set a field to an object. We expand on this in case 2. // Before_({name:'Tomer',age:23})// After_({name:'Tomer'}).extend({age:23})// Creates a new object and doesn't modify the original in-place.// {name: 'Tomer', age: 23} Case 2Expanding on case 1, functionally add/set a field based on the object's current values: // Beforeconstbirthday=moment('10/04/2001');_({name:'Tomer', birthday,age:birthday.diff(moment(),'years')})// After_({name:'Tomer',birthday:moment('10/04/2001')}).extend(obj=>({age:obj.birthday.diff(moment(),'years')}))// {name: 'Tomer', birthday: <moment>, age: 23} Case 3Expanding on case 2, applying the extend function to each object in an array: _(people).extend(obj=>({age:obj.birthday.diff(moment(),'years')}))// Applies the previously mentioned `extend` to each of the objects in the list// Basically .map(.extend()) I know there's an existing If I would proceed with creating a PR, maybe adding a differently named function will be more fitting. |
BetaWas this translation helpful?Give feedback.