Proposal: syntactic sugar for extracting fields from objects

guest271314 guest271314 at gmail.com
Tue May 28 18:49:06 UTC 2019


>
> ```
>> 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 <rtm at gol.com> 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
> 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/20190528/77775cc0/attachment.html>


More information about the es-discuss mailing list