Re: Proposal: syntactic sugar for extracting fields from objects
In this case you can utilize a single step by setting a default value for ```otherData``` ```let {firstName, lastName, otherData = "otherData"} = user.profile;``` On Sun, May 26, 2019 at 2:56 PM Григорий Карелин wrote: > Yep, in the same way as destructuring would work > > вс, 26 мая 2019 г. в 17:52, guest271314 : > >> If not found in source ```firstName``` and/or ```lastName``` would be >> assigned the value ```undefined```? >> >> On Sun, May 26, 2019 at 1:40 PM Григорий Карелин >> wrote: >> >>> Wouldn't it be nice to have syntax like this: >>> const obj = { {firstName, lastName from user.profile}, otherData: 'other >>> data' }; >>> as a syntactic sugar for >>> const obj = {firstName: user.profile.firstName, lastName: >>> user.profile.lastName, otherData: 'other data'}; >>> >>> Of cause at the moment we can write it in two steps: >>> const {fistName, lastName} = userProfile; >>> const obj = {firstName, lastName, otherData: 'other data'} >>> >>> But why use extra variables? >>> >>> Motivating example is lodash's .pick() method: >>> https://lodash.com/docs/#pick >>> ___ >>> es-discuss mailing list >>> es-discuss@mozilla.org >>> https://mail.mozilla.org/listinfo/es-discuss >>> >> > > -- > С уважением, > Карелин Григорий > ___ es-discuss mailing list es-discuss@mozilla.org https://mail.mozilla.org/listinfo/es-discuss
Proposal: syntactic sugar for extracting fields from objects
Similar proposals have been out there for years. For whatever reason, none have gotten traction. Under one proposal, your scenario would be ``` const obj ={...user_profile.{firstName, lastName}, otherData: 'other data'}; ``` https://github.com/rtm/js-pick-notation Bob -- Forwarded message -- > From: "Григорий Карелин" > To: es-discuss@mozilla.org > Cc: > Bcc: > Date: Sun, 26 May 2019 16:39:58 +0300 > Subject: Proposal: syntactic sugar for extracting fields from objects > Wouldn't it be nice to have syntax like this: > const obj = { {firstName, lastName from user.profile}, otherData: 'other > data' }; > as a syntactic sugar for > const obj = {firstName: user.profile.firstName, lastName: > user.profile.lastName, otherData: 'other data'}; > > Of cause at the moment we can write it in two steps: > const {fistName, lastName} = userProfile; > const obj = {firstName, lastName, otherData: 'other data'} > > But why use extra variables? > > Motivating example is lodash's .pick() method: > https://lodash.com/docs/#pick > ___ es-discuss mailing list es-discuss@mozilla.org https://mail.mozilla.org/listinfo/es-discuss
Re: Proposal: syntactic sugar for extracting fields from objects
I tried to see if I could do this in a single destructuring step, but here is what happened: ``` var user = { profile: { firstName: 'Bob', lastName: 'Ruffward', x: 'hi' } } var obj = { ...({firstName, lastName} = user.profile), otherData: 'otherData' } ``` So... what happened? (I'm sure you all know already) `obj` ended up with _all_ of `user.profile`'s properties: ``` { firstName: "Bob", lastName: "Ruffward", x: "hi", otherData: "otherData" } ``` and `firstName` and `lastName` were assigned as global variables. ``` firstName \\ "Bob" lastName \\ "Ruffward" ``` -- Dammit babies, you've got to be kind. On Sun, 26 May 2019 at 15:56, Григорий Карелин wrote: > Yep, in the same way as destructuring would work > > вс, 26 мая 2019 г. в 17:52, guest271314 : > >> If not found in source ```firstName``` and/or ```lastName``` would be >> assigned the value ```undefined```? >> >> On Sun, May 26, 2019 at 1:40 PM Григорий Карелин >> wrote: >> >>> Wouldn't it be nice to have syntax like this: >>> const obj = { {firstName, lastName from user.profile}, otherData: 'other >>> data' }; >>> as a syntactic sugar for >>> const obj = {firstName: user.profile.firstName, lastName: >>> user.profile.lastName, otherData: 'other data'}; >>> >>> Of cause at the moment we can write it in two steps: >>> const {fistName, lastName} = userProfile; >>> const obj = {firstName, lastName, otherData: 'other data'} >>> >>> But why use extra variables? >>> >>> Motivating example is lodash's .pick() method: >>> https://lodash.com/docs/#pick >>> ___ >>> es-discuss mailing list >>> es-discuss@mozilla.org >>> https://mail.mozilla.org/listinfo/es-discuss >>> >> > > -- > С уважением, > Карелин Григорий > ___ > es-discuss mailing list > es-discuss@mozilla.org > https://mail.mozilla.org/listinfo/es-discuss > ___ es-discuss mailing list es-discuss@mozilla.org https://mail.mozilla.org/listinfo/es-discuss
Re: Proposal: syntactic sugar for extracting fields from objects
Yep, in the same way as destructuring would work вс, 26 мая 2019 г. в 17:52, guest271314 : > If not found in source ```firstName``` and/or ```lastName``` would be > assigned the value ```undefined```? > > On Sun, May 26, 2019 at 1:40 PM Григорий Карелин > wrote: > >> Wouldn't it be nice to have syntax like this: >> const obj = { {firstName, lastName from user.profile}, otherData: 'other >> data' }; >> as a syntactic sugar for >> const obj = {firstName: user.profile.firstName, lastName: >> user.profile.lastName, otherData: 'other data'}; >> >> Of cause at the moment we can write it in two steps: >> const {fistName, lastName} = userProfile; >> const obj = {firstName, lastName, otherData: 'other data'} >> >> But why use extra variables? >> >> Motivating example is lodash's .pick() method: >> https://lodash.com/docs/#pick >> ___ >> es-discuss mailing list >> es-discuss@mozilla.org >> https://mail.mozilla.org/listinfo/es-discuss >> > -- С уважением, Карелин Григорий ___ es-discuss mailing list es-discuss@mozilla.org https://mail.mozilla.org/listinfo/es-discuss
Re: Proposal: syntactic sugar for extracting fields from objects
If not found in source ```firstName``` and/or ```lastName``` would be assigned the value ```undefined```? On Sun, May 26, 2019 at 1:40 PM Григорий Карелин wrote: > Wouldn't it be nice to have syntax like this: > const obj = { {firstName, lastName from user.profile}, otherData: 'other > data' }; > as a syntactic sugar for > const obj = {firstName: user.profile.firstName, lastName: > user.profile.lastName, otherData: 'other data'}; > > Of cause at the moment we can write it in two steps: > const {fistName, lastName} = userProfile; > const obj = {firstName, lastName, otherData: 'other data'} > > But why use extra variables? > > Motivating example is lodash's .pick() method: > https://lodash.com/docs/#pick > ___ > es-discuss mailing list > es-discuss@mozilla.org > https://mail.mozilla.org/listinfo/es-discuss > ___ es-discuss mailing list es-discuss@mozilla.org https://mail.mozilla.org/listinfo/es-discuss
Proposal: syntactic sugar for extracting fields from objects
Wouldn't it be nice to have syntax like this: const obj = { {firstName, lastName from user.profile}, otherData: 'other data' }; as a syntactic sugar for const obj = {firstName: user.profile.firstName, lastName: user.profile.lastName, otherData: 'other data'}; Of cause at the moment we can write it in two steps: const {fistName, lastName} = userProfile; const obj = {firstName, lastName, otherData: 'other data'} But why use extra variables? Motivating example is lodash's .pick() method: https://lodash.com/docs/#pick ___ es-discuss mailing list es-discuss@mozilla.org https://mail.mozilla.org/listinfo/es-discuss