Proposal: syntactic sugar for extracting fields from objects

Bob Myers rtm at gol.com
Tue May 28 14:46:10 UTC 2019


>
> ```
> let obj = {otherData: "other data"};
> ({firstName:obj.firstName, lastName:obj.lastName} = user.profile);
> ```
>

I don't understand this.


> Alternatively there are various approaches which can be used to get only
> specific properties from an abject and set those properties and values at a
> new object without using destructing assignment.
>
> Using object rest and ```reduce()```
>
> ```let obj = {otherData: "other data", ...["firstName",
> "lastName"].reduce((o, prop) => (o[prop] = user.profile[prop], o), {})};```
>
> `Object.assign()`, spread syntax and `map()`
>
> ```let obj = Object.assign({otherData: "other data"}, ...["firstName",
> "lastName"].map(prop => ({[prop]:user.profile[prop]})));```
>

As the words "syntactic sugar" in the subject of the thread make clear, the
OP is not merely looking for ways to assign one object's property into
another--there are many ways to do that. He's looking for a terser, moire
elegant way to do it, which hopefully would be moire semantic, less
bug-prone, more type-checkable (for typed variants of the language), more
reminiscent of existing syntax such as deconstruction, and potentially more
optimizable by engines.

Bob
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20190528/15363cd6/attachment.html>


More information about the es-discuss mailing list