Proposal: syntactic sugar for extracting fields from objects

classic Classic list List threaded Threaded
37 messages Options
12
Reply | Threaded
Open this post in threaded view
|

Proposal: syntactic sugar for extracting fields from objects

Григорий Карелин
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
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Proposal: syntactic sugar for extracting fields from objects

guest271314
If not found in source ```firstName``` and/or ```lastName``` would be assigned the value ```undefined```?

On Sun, May 26, 2019 at 1:40 PM Григорий Карелин <[hidden email]> 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
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss

_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Proposal: syntactic sugar for extracting fields from objects

Григорий Карелин
Yep, in the same way as destructuring would work

вс, 26 мая 2019 г. в 17:52, guest271314 <[hidden email]>:
If not found in source ```firstName``` and/or ```lastName``` would be assigned the value ```undefined```?

On Sun, May 26, 2019 at 1:40 PM Григорий Карелин <[hidden email]> 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
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss


--
С уважением,
Карелин Григорий

_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Proposal: syntactic sugar for extracting fields from objects

Michael Luder-Rosefield
I tried to see if I could do this in a single destructuring step, but here is what happened:

```
var user = { profile: { firstName: 'Bob', lastName: 'Ruffward', x: 'hi' } }
var obj = { ...({firstName, lastName} = user.profile), otherData: 'otherData' }
```

So... what happened? (I'm sure you all know already)

`obj` ended up with _all_ of `user.profile`'s properties:
```
{ firstName: "Bob", lastName: "Ruffward", x: "hi", otherData: "otherData" }
```
and `firstName` and `lastName` were assigned as global variables.

```
firstName \\ "Bob"
lastName \\ "Ruffward"
```

--------------------------
Dammit babies, you've got to be kind.


On Sun, 26 May 2019 at 15:56, Григорий Карелин <[hidden email]> wrote:
Yep, in the same way as destructuring would work

вс, 26 мая 2019 г. в 17:52, guest271314 <[hidden email]>:
If not found in source ```firstName``` and/or ```lastName``` would be assigned the value ```undefined```?

On Sun, May 26, 2019 at 1:40 PM Григорий Карелин <[hidden email]> 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
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss


--
С уважением,
Карелин Григорий
_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss

_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Proposal: syntactic sugar for extracting fields from objects

guest271314
In reply to this post by Григорий Карелин
In this case you can utilize a single step by setting a default value for ```otherData```

```let {firstName, lastName, otherData = "otherData"} = user.profile;```


On Sun, May 26, 2019 at 2:56 PM Григорий Карелин <[hidden email]> wrote:
Yep, in the same way as destructuring would work

вс, 26 мая 2019 г. в 17:52, guest271314 <[hidden email]>:
If not found in source ```firstName``` and/or ```lastName``` would be assigned the value ```undefined```?

On Sun, May 26, 2019 at 1:40 PM Григорий Карелин <[hidden email]> 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
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss


--
С уважением,
Карелин Григорий

_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Proposal: syntactic sugar for extracting fields from objects

guest271314
In reply to this post by Григорий Карелин
But why use extra variables?

Is the expected result for ```obj``` to be defined as a variable and ```firstName``` and ```lastName``` to not be defined as variables?

On Sun, May 26, 2019 at 2:56 PM Григорий Карелин <[hidden email]> wrote:
Yep, in the same way as destructuring would work

вс, 26 мая 2019 г. в 17:52, guest271314 <[hidden email]>:
If not found in source ```firstName``` and/or ```lastName``` would be assigned the value ```undefined```?

On Sun, May 26, 2019 at 1:40 PM Григорий Карелин <[hidden email]> 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
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss


--
С уважением,
Карелин Григорий

_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Proposal: syntactic sugar for extracting fields from objects

Григорий Карелин
> 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
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Proposal: syntactic sugar for extracting fields from objects

guest271314


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 Григорий Карелин <[hidden email]> 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
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Proposal: syntactic sugar for extracting fields from objects

Bob Myers
In reply to this post by Григорий Карелин
```
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

_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Proposal: syntactic sugar for extracting fields from objects

guest271314
```
let obj = {otherData: "other data"};
({firstName:obj.firstName, lastName:obj.lastName} = user.profile);
``` 
I don't understand this.

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

He's looking for a terser

Is the proposal to golf destructuring assignment? 

The proposed destructuring assignment syntax example is 25 characters less than the non-destructing assignment example, which is terser.

One observation about the proposed syntax is that the values set at the target object would be set from the identifier on the left side of ```from``` which is similar to 

```
var o = {x:1};
console.log(o.x = 2); // 2
```

though how will the property name be set at the object at target object instead of `{"2":2}`? How does the engine know when the expected result is ```{"x":2}``` and not ```{"2":2}```? Certainly such functionality can be designed, for example, using the proposed key word ```from```. 

If more terse code is one of the features that would be achieved, why are the wrapping `{}` around ```from`` necessary?

> moire elegant way to do it, 

"elegant" is subjective

> 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.

What is bug prone about the code examples at OP?

This proposal would resolve the issue of currently, in general, having to write the property name twice.



On Tue, May 28, 2019 at 2:47 PM Bob Myers <[hidden email]> wrote:
```
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
_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss

_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Proposal: syntactic sugar for extracting fields from objects

Григорий Карелин
Here are another examples, where "destructuring picking" I suggest whould be helpful.
==1A Now (redux related)
```
function mapStateToProps({user: {firstName, lastName}, common: {currentPageURL: url}}) {
  return {firstName, lastName, url};
}
```
==1B Proposal
```
function mapStateToProps(state) {
  return {{firstName, lastName from state.user}, {currentPageURL as url from state.common}};
}
```

Shorter!

==2A Now
```
async function saveNewUserName(name) {
  const {name} = await sendToServer({name});

  return {ok: true, payload: {name}}; // oh wait, which name is it again? Argument or response?
}
```
== 2B Proposal
```
async function saveNewUserName(name) {
  const resp = await sendToServer({name});

  return {ok: true, {name from response}};
}
```
No accidental shadowing.

I know, I know, you can achieve all that without new syntax, via naming your variables properly and using long explicit expressions. But I think some sugar won't hurt.
After all, remember destructuring? There's no reason to use it other than it's cool and short and expressive.


вт, 28 мая 2019 г. в 21:49, guest271314 <[hidden email]>:
```
let obj = {otherData: "other data"};
({firstName:obj.firstName, lastName:obj.lastName} = user.profile);
``` 
I don't understand this.

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

He's looking for a terser

Is the proposal to golf destructuring assignment? 

The proposed destructuring assignment syntax example is 25 characters less than the non-destructing assignment example, which is terser.

One observation about the proposed syntax is that the values set at the target object would be set from the identifier on the left side of ```from``` which is similar to 

```
var o = {x:1};
console.log(o.x = 2); // 2
```

though how will the property name be set at the object at target object instead of `{"2":2}`? How does the engine know when the expected result is ```{"x":2}``` and not ```{"2":2}```? Certainly such functionality can be designed, for example, using the proposed key word ```from```. 

If more terse code is one of the features that would be achieved, why are the wrapping `{}` around ```from`` necessary?

> moire elegant way to do it, 

"elegant" is subjective

> 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.

What is bug prone about the code examples at OP?

This proposal would resolve the issue of currently, in general, having to write the property name twice.



On Tue, May 28, 2019 at 2:47 PM Bob Myers <[hidden email]> wrote:
```
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
_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss


--
С уважением,
Карелин Григорий

_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Proposal: syntactic sugar for extracting fields from objects

Cyril Auburtin
What I'd like is this syntax:

```js
let o={a:1, b:2, c:3};
o.{a, b} // would be {a: 1, b: 2}
```

On Tue, May 28, 2019 at 9:13 PM Григорий Карелин <[hidden email]> wrote:
Here are another examples, where "destructuring picking" I suggest whould be helpful.
==1A Now (redux related)
```
function mapStateToProps({user: {firstName, lastName}, common: {currentPageURL: url}}) {
  return {firstName, lastName, url};
}
```
==1B Proposal
```
function mapStateToProps(state) {
  return {{firstName, lastName from state.user}, {currentPageURL as url from state.common}};
}
```

Shorter!

==2A Now
```
async function saveNewUserName(name) {
  const {name} = await sendToServer({name});

  return {ok: true, payload: {name}}; // oh wait, which name is it again? Argument or response?
}
```
== 2B Proposal
```
async function saveNewUserName(name) {
  const resp = await sendToServer({name});

  return {ok: true, {name from response}};
}
```
No accidental shadowing.

I know, I know, you can achieve all that without new syntax, via naming your variables properly and using long explicit expressions. But I think some sugar won't hurt.
After all, remember destructuring? There's no reason to use it other than it's cool and short and expressive.


вт, 28 мая 2019 г. в 21:49, guest271314 <[hidden email]>:
```
let obj = {otherData: "other data"};
({firstName:obj.firstName, lastName:obj.lastName} = user.profile);
``` 
I don't understand this.

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

He's looking for a terser

Is the proposal to golf destructuring assignment? 

The proposed destructuring assignment syntax example is 25 characters less than the non-destructing assignment example, which is terser.

One observation about the proposed syntax is that the values set at the target object would be set from the identifier on the left side of ```from``` which is similar to 

```
var o = {x:1};
console.log(o.x = 2); // 2
```

though how will the property name be set at the object at target object instead of `{"2":2}`? How does the engine know when the expected result is ```{"x":2}``` and not ```{"2":2}```? Certainly such functionality can be designed, for example, using the proposed key word ```from```. 

If more terse code is one of the features that would be achieved, why are the wrapping `{}` around ```from`` necessary?

> moire elegant way to do it, 

"elegant" is subjective

> 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.

What is bug prone about the code examples at OP?

This proposal would resolve the issue of currently, in general, having to write the property name twice.



On Tue, May 28, 2019 at 2:47 PM Bob Myers <[hidden email]> wrote:
```
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
_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss


--
С уважением,
Карелин Григорий
_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss

_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Proposal: syntactic sugar for extracting fields from objects

Григорий Карелин
Hi Cyril,
With the syntax you propose what would be the way to to this:
```
const source = {foo: 1, bar: 2};
const result = {foo: source.foo, bar: source.bar, buzz: 3}
```
?

ср, 29 мая 2019 г. в 20:14, Cyril Auburtin <[hidden email]>:
What I'd like is this syntax:

```js
let o={a:1, b:2, c:3};
o.{a, b} // would be {a: 1, b: 2}
```

On Tue, May 28, 2019 at 9:13 PM Григорий Карелин <[hidden email]> wrote:
Here are another examples, where "destructuring picking" I suggest whould be helpful.
==1A Now (redux related)
```
function mapStateToProps({user: {firstName, lastName}, common: {currentPageURL: url}}) {
  return {firstName, lastName, url};
}
```
==1B Proposal
```
function mapStateToProps(state) {
  return {{firstName, lastName from state.user}, {currentPageURL as url from state.common}};
}
```

Shorter!

==2A Now
```
async function saveNewUserName(name) {
  const {name} = await sendToServer({name});

  return {ok: true, payload: {name}}; // oh wait, which name is it again? Argument or response?
}
```
== 2B Proposal
```
async function saveNewUserName(name) {
  const resp = await sendToServer({name});

  return {ok: true, {name from response}};
}
```
No accidental shadowing.

I know, I know, you can achieve all that without new syntax, via naming your variables properly and using long explicit expressions. But I think some sugar won't hurt.
After all, remember destructuring? There's no reason to use it other than it's cool and short and expressive.


вт, 28 мая 2019 г. в 21:49, guest271314 <[hidden email]>:
```
let obj = {otherData: "other data"};
({firstName:obj.firstName, lastName:obj.lastName} = user.profile);
``` 
I don't understand this.

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

He's looking for a terser

Is the proposal to golf destructuring assignment? 

The proposed destructuring assignment syntax example is 25 characters less than the non-destructing assignment example, which is terser.

One observation about the proposed syntax is that the values set at the target object would be set from the identifier on the left side of ```from``` which is similar to 

```
var o = {x:1};
console.log(o.x = 2); // 2
```

though how will the property name be set at the object at target object instead of `{"2":2}`? How does the engine know when the expected result is ```{"x":2}``` and not ```{"2":2}```? Certainly such functionality can be designed, for example, using the proposed key word ```from```. 

If more terse code is one of the features that would be achieved, why are the wrapping `{}` around ```from`` necessary?

> moire elegant way to do it, 

"elegant" is subjective

> 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.

What is bug prone about the code examples at OP?

This proposal would resolve the issue of currently, in general, having to write the property name twice.



On Tue, May 28, 2019 at 2:47 PM Bob Myers <[hidden email]> wrote:
```
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
_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss


--
С уважением,
Карелин Григорий
_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss


--
С уважением,
Карелин Григорий

_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Proposal: syntactic sugar for extracting fields from objects

Bob Myers
On Wed, May 29, 2019 at 10:28 AM Григорий Карелин <[hidden email]> wrote:
Hi Cyril,
With the syntax you propose what would be the way to to this:
```
const source = {foo: 1, bar: 2};
const result = {foo: source.foo, bar: source.bar, buzz: 3}
```
?

As already mentioned earlier in this thread:

```js
const source = {foo: 1, bar: 2};
const result = {...source.{foo, bar}, buzz: 3};
```

_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Proposal: syntactic sugar for extracting fields from objects

Григорий Карелин
Well, I guess it might work too.

I personally like more verbose constructions, with keywords instead of dots, asterisks, etc :)

On Wed, 29 May 2019 at 21:03, Bob Myers <[hidden email]> wrote:
On Wed, May 29, 2019 at 10:28 AM Григорий Карелин <[hidden email]> wrote:
Hi Cyril,
With the syntax you propose what would be the way to to this:
```
const source = {foo: 1, bar: 2};
const result = {foo: source.foo, bar: source.bar, buzz: 3}
```
?

As already mentioned earlier in this thread:

```js
const source = {foo: 1, bar: 2};
const result = {...source.{foo, bar}, buzz: 3};
```
--
Отправлено с мобильного устройства

_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Proposal: syntactic sugar for extracting fields from objects

Bob Myers
This syntax merely combines and extends two existing notations:

1. Dot notation to access properties, which has been in JS since it was initially designed, with the RHS extended to permit a set of properties in `{}` in addition to a plain old identifier as at present.
2. Property spread notation (`{...{}}`).

On Wed, May 29, 2019 at 11:27 AM Григорий Карелин <[hidden email]> wrote:
Well, I guess it might work too.

I personally like more verbose constructions, with keywords instead of dots, asterisks, etc :)

On Wed, 29 May 2019 at 21:03, Bob Myers <[hidden email]> wrote:
On Wed, May 29, 2019 at 10:28 AM Григорий Карелин <[hidden email]> wrote:
Hi Cyril,
With the syntax you propose what would be the way to to this:
```
const source = {foo: 1, bar: 2};
const result = {foo: source.foo, bar: source.bar, buzz: 3}
```
?

As already mentioned earlier in this thread:

```js
const source = {foo: 1, bar: 2};
const result = {...source.{foo, bar}, buzz: 3};
```
--
Отправлено с мобильного устройства

_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Proposal: syntactic sugar for extracting fields from objects

Григорий Карелин
I agree.

So, what does community think? Do we want to have “destructuring picking” sugar in JS and if we do, which syntax looks more attractive?

I’d suggest to vote.


On Wed, 29 May 2019 at 21:55, Bob Myers <[hidden email]> wrote:
This syntax merely combines and extends two existing notations:

1. Dot notation to access properties, which has been in JS since it was initially designed, with the RHS extended to permit a set of properties in `{}` in addition to a plain old identifier as at present.
2. Property spread notation (`{...{}}`).

On Wed, May 29, 2019 at 11:27 AM Григорий Карелин <[hidden email]> wrote:
Well, I guess it might work too.

I personally like more verbose constructions, with keywords instead of dots, asterisks, etc :)

On Wed, 29 May 2019 at 21:03, Bob Myers <[hidden email]> wrote:
On Wed, May 29, 2019 at 10:28 AM Григорий Карелин <[hidden email]> wrote:
Hi Cyril,
With the syntax you propose what would be the way to to this:
```
const source = {foo: 1, bar: 2};
const result = {foo: source.foo, bar: source.bar, buzz: 3}
```
?

As already mentioned earlier in this thread:

```js
const source = {foo: 1, bar: 2};
const result = {...source.{foo, bar}, buzz: 3};
```
--
Отправлено с мобильного устройства
--
Отправлено с мобильного устройства

_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Proposal: syntactic sugar for extracting fields from objects

Oriol _

If you want to add this you will need a champion, see https://github.com/tc39/ecma262/blob/master/CONTRIBUTING.md#new-feature-proposals

-- Oriol


El 29/5/19 a les 21:15, Григорий Карелин ha escrit:
I agree.

So, what does community think? Do we want to have “destructuring picking” sugar in JS and if we do, which syntax looks more attractive?

I’d suggest to vote.
--
Отправлено с мобильного устройства


_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss


_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Proposal: syntactic sugar for extracting fields from objects

Григорий Карелин
True

I think it's possible to find someone who will represent the will of community.

At the moment the question is does community have will to add proposed sugar to the language, and if so, which version.

ср, 29 мая 2019 г. в 22:30, Oriol _ <[hidden email]>:

If you want to add this you will need a champion, see https://github.com/tc39/ecma262/blob/master/CONTRIBUTING.md#new-feature-proposals

-- Oriol


El 29/5/19 a les 21:15, Григорий Карелин ha escrit:
I agree.

So, what does community think? Do we want to have “destructuring picking” sugar in JS and if we do, which syntax looks more attractive?

I’d suggest to vote.
--
Отправлено с мобильного устройства


_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss

_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss


--
С уважением,
Карелин Григорий

_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Proposal: syntactic sugar for extracting fields from objects

guest271314
> I think it's possible to find someone who will represent the will of community.

Individuals can compose the code right now.

> At the moment the question is does community have will to add proposed sugar to the language, and if so, which version.

Why would there be any restriction on the versions of syntax which would achieve the requirement? The original proposal using ```from``` and other proposals could each be created, tested, specified.

On Wed, May 29, 2019 at 7:37 PM Григорий Карелин <[hidden email]> wrote:
True

I think it's possible to find someone who will represent the will of community.

At the moment the question is does community have will to add proposed sugar to the language, and if so, which version.

ср, 29 мая 2019 г. в 22:30, Oriol _ <[hidden email]>:

If you want to add this you will need a champion, see https://github.com/tc39/ecma262/blob/master/CONTRIBUTING.md#new-feature-proposals

-- Oriol


El 29/5/19 a les 21:15, Григорий Карелин ha escrit:
I agree.

So, what does community think? Do we want to have “destructuring picking” sugar in JS and if we do, which syntax looks more attractive?

I’d suggest to vote.
--
Отправлено с мобильного устройства


_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss

_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss


--
С уважением,
Карелин Григорий
_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss

_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
12