Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Does lodash have functions for constructing (i.e. building brick by brick) an object?#5865

Unanswered
tomerh2001 asked this question inQ&A
Discussion options

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 1

Functionally 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 2

Expanding 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 3

Expanding 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 existingextend function that modifies the target object in place instead of creating a new object. It also doesn't support dynamic assignment (case 2) or an array equivalent (case 3).

If I would proceed with creating a PR, maybe adding a differently named function will be more fitting.

You must be logged in to vote

Replies: 0 comments

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Category
Q&A
Labels
None yet
1 participant
@tomerh2001

[8]ページ先頭

©2009-2025 Movatter.jp