Proposal: syntactic sugar for extracting fields from objects

guest271314 guest271314 at
Mon May 27 19:04:14 UTC 2019

To use destructuring assignment and not create additional variables you can
assign the value of target to a previously defined object

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

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]})));```

or the original approach at the OP.

On Mon, May 27, 2019 at 1:44 PM Григорий Карелин <grundiss at> wrote:

> > Is the expected result for ```obj``` to be defined as a variable and
> ```firstName``` and ```lastName``` to not be defined as variables?
> Exactly
> As you mentioned above, we may use default value while destructuring to
> achieve the same result, but it looks quite unobvious and creates extra
> variables
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <>

More information about the es-discuss mailing list