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 Григорий Карелин <[email protected]> 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
>
_______________________________________________
es-discuss mailing list
[email protected]
https://mail.mozilla.org/listinfo/es-discuss