Proposal: syntactic sugar for extracting fields from objects

guest271314 guest271314 at gmail.com
Sun Jun 2 00:27:52 UTC 2019


```from``` implementation using JavaScript

```
const user = {profile:{firstName:"00", lastName:"11"}};
const from = (o, ...props) =>
Object.fromEntries(Object.entries(o).filter(([key]) =>
props.includes(key)));
let obj = {otherData:'other data',...from(user.profile, "firstName")};
```

which should be possible to code as

```
const user = {profile:{firstName:"00", lastName:"11"}};
 // preserve right-side/last value syntax to avoid confusion
let obj = {otherData:'other data',...{ from user.profile {firstName} }};
```

or, similar to
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment#Assigning_to_new_variable_names

```
const user = {profile:{firstName:"00", lastName:"11"}};
let obj = {otherData:'other data'};
({firstName /* , lastName, ...props */} from user.profile to obj);
```



On Sun, May 26, 2019 at 1:40 PM Григорий Карелин <grundiss at gmail.com> 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 at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20190602/3b602346/attachment.html>


More information about the es-discuss mailing list