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

Reply via email to