Extend Object Dereferencing

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

Extend Object Dereferencing

Sebastian Malton
Currently you can do the following 

const {abc, xyz, qnc} = obj;

However if you want to go more than one level deep then you have to do it again for each level.

I therefore propose the ability to do the following 

const {abc, xyz, qnc.awj} = obj;

And this would create the variables 'abc'', 'xyz', 'awj' with the values from obj. If qnc is not present then an undefined dereference exception would be thrown. 

This could also be extended with the proposed '?.' notation to go even deeper safely. 

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

Re: Extend Object Dereferencing

Sebastian Malton
This could also be extended to help with cleansing objects.

const abc = {cde, def, efg} = obj;

Would create an object abc with only those three fields in it.

Sent: October 24, 2017 2:51 PM
Subject: Extend Object Dereferencing

Currently you can do the following 

const {abc, xyz, qnc} = obj;

However if you want to go more than one level deep then you have to do it again for each level.

I therefore propose the ability to do the following 

const {abc, xyz, qnc.awj} = obj;

And this would create the variables 'abc'', 'xyz', 'awj' with the values from obj. If qnc is not present then an undefined dereference exception would be thrown. 

This could also be extended with the proposed '?.' notation to go even deeper safely. 

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

Re: Extend Object Dereferencing

Bob Myers
Don't you just mean the following:

```
const {abc, xyz, qnc: {awj}} = obj;
```

On Wed, Oct 25, 2017 at 12:22 AM, Sebastian Malton <[hidden email]> wrote:
This could also be extended to help with cleansing objects.

const abc = {cde, def, efg} = obj;

Would create an object abc with only those three fields in it.

Sent: October 24, 2017 2:51 PM
Subject: Extend Object Dereferencing

Currently you can do the following 

const {abc, xyz, qnc} = obj;

However if you want to go more than one level deep then you have to do it again for each level.

I therefore propose the ability to do the following 

const {abc, xyz, qnc.awj} = obj;

And this would create the variables 'abc'', 'xyz', 'awj' with the values from obj. If qnc is not present then an undefined dereference exception would be thrown. 

This could also be extended with the proposed '?.' notation to go even deeper safely. 

_______________________________________________
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: Extend Object Dereferencing

Sebastian Malton
I guess I do except for the second part

```
const a = {b, c, d} = e;
```

Sent: October 24, 2017 3:07 PM
Subject: Re: Extend Object Dereferencing

Don't you just mean the following:

```
const {abc, xyz, qnc: {awj}} = obj;
```

On Wed, Oct 25, 2017 at 12:22 AM, Sebastian Malton <[hidden email]> wrote:
This could also be extended to help with cleansing objects.

const abc = {cde, def, efg} = obj;

Would create an object abc with only those three fields in it.

Sent: October 24, 2017 2:51 PM
Subject: Extend Object Dereferencing

Currently you can do the following 

const {abc, xyz, qnc} = obj;

However if you want to go more than one level deep then you have to do it again for each level.

I therefore propose the ability to do the following 

const {abc, xyz, qnc.awj} = obj;

And this would create the variables 'abc'', 'xyz', 'awj' with the values from obj. If qnc is not present then an undefined dereference exception would be thrown. 

This could also be extended with the proposed '?.' notation to go even deeper safely. 

_______________________________________________
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: Extend Object Dereferencing

Bob Myers
In reply to this post by Bob Myers
If your intent is to say

```
const a = {b: e.b, c: e.c, d: e.d};
```

then the reception to such functionality (often referred to as "picking") on this list has been decidedly lukewarm, although I remain mystified why, since IMHO it is a common use case and syntactically straightforward. FWIW the currently proposed syntax is

```
const a = { {b, c, d} = e };
```

which allows combining properties from more than one object, as in

```
const a = { {b, c, d} = e, {g, h, i} = k };
```

which is equivalent to 

```
const a = {b: e.b, c: e.c, d: e.d, g: k.g, h: k.h, i: k.i};
```

Bob

On Wed, Oct 25, 2017 at 12:39 AM, Sebastian Malton <[hidden email]> wrote:
I guess I do except for the second part

```
const a = {b, c, d} = e;
```

Sent: October 24, 2017 3:07 PM
Subject: Re: Extend Object Dereferencing

Don't you just mean the following:

```
const {abc, xyz, qnc: {awj}} = obj;
```

On Wed, Oct 25, 2017 at 12:22 AM, Sebastian Malton <[hidden email]> wrote:
This could also be extended to help with cleansing objects.

const abc = {cde, def, efg} = obj;

Would create an object abc with only those three fields in it.

Sent: October 24, 2017 2:51 PM
Subject: Extend Object Dereferencing

Currently you can do the following 

const {abc, xyz, qnc} = obj;

However if you want to go more than one level deep then you have to do it again for each level.

I therefore propose the ability to do the following 

const {abc, xyz, qnc.awj} = obj;

And this would create the variables 'abc'', 'xyz', 'awj' with the values from obj. If qnc is not present then an undefined dereference exception would be thrown. 

This could also be extended with the proposed '?.' notation to go even deeper safely. 

_______________________________________________
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: Extend Object Dereferencing

Sebastian Malton
I see, I can understand how the currently proposed syntax is more powerful but I can also see people saying that is the base case with only one object it seems weird to have the additional curly braces

Sebastian 

Sent: October 24, 2017 3:26 PM
Subject: Re: Extend Object Dereferencing

If your intent is to say

```
const a = {b: e.b, c: e.c, d: e.d};
```

then the reception to such functionality (often referred to as "picking") on this list has been decidedly lukewarm, although I remain mystified why, since IMHO it is a common use case and syntactically straightforward. FWIW the currently proposed syntax is

```
const a = { {b, c, d} = e };
```

which allows combining properties from more than one object, as in

```
const a = { {b, c, d} = e, {g, h, i} = k };
```

which is equivalent to 

```
const a = {b: e.b, c: e.c, d: e.d, g: k.g, h: k.h, i: k.i};
```

Bob

On Wed, Oct 25, 2017 at 12:39 AM, Sebastian Malton <[hidden email]> wrote:
I guess I do except for the second part

```
const a = {b, c, d} = e;
```

Sent: October 24, 2017 3:07 PM
Subject: Re: Extend Object Dereferencing

Don't you just mean the following:

```
const {abc, xyz, qnc: {awj}} = obj;
```

On Wed, Oct 25, 2017 at 12:22 AM, Sebastian Malton <[hidden email]> wrote:
This could also be extended to help with cleansing objects.

const abc = {cde, def, efg} = obj;

Would create an object abc with only those three fields in it.

Sent: October 24, 2017 2:51 PM
Subject: Extend Object Dereferencing

Currently you can do the following 

const {abc, xyz, qnc} = obj;

However if you want to go more than one level deep then you have to do it again for each level.

I therefore propose the ability to do the following 

const {abc, xyz, qnc.awj} = obj;

And this would create the variables 'abc'', 'xyz', 'awj' with the values from obj. If qnc is not present then an undefined dereference exception would be thrown. 

This could also be extended with the proposed '?.' notation to go even deeper safely. 

_______________________________________________
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: Extend Object Dereferencing

T.J. Crowder-2
In reply to this post by Sebastian Malton
On Tue, Oct 24, 2017 at 7:51 PM, Sebastian Malton
>
> Currently you can do the following 
>
> const {abc, xyz, qnc} = obj;
>
> However if you want to go more than one level deep then you have
> to do it again for each level.

You don't have to do it again, you can nest patterns as deeply as you like. In this case:

```js
const {abc, xyz, qnc: {awj}} = obj;
```j


```js
const obj = {
    abc: 1,
    xyz: 2,
    qnc: {
        awj: 3
    }
};
const {abc, xyz, qnc: {awj}} = obj;
console.log(abc, xyz, awj); // 1, 2, 3
```js

-- T.J. Crowder


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

Re: Extend Object Dereferencing

Sebastian Malton
I didn't mean that part when I said multiple levels. I meant the following 

```
const abc = { { bcd, cde, efg: {qnc} } = obj};

```

This would most definitely seem strange as to why the outer {} is needed for picking from a single object 

Sent: October 25, 2017 8:10 AM
Subject: Re: Extend Object Dereferencing

On Tue, Oct 24, 2017 at 7:51 PM, Sebastian Malton
>
> Currently you can do the following 
>
> const {abc, xyz, qnc} = obj;
>
> However if you want to go more than one level deep then you have
> to do it again for each level.

You don't have to do it again, you can nest patterns as deeply as you like. In this case:

```js
const {abc, xyz, qnc: {awj}} = obj;
```j


```js
const obj = {
    abc: 1,
    xyz: 2,
    qnc: {
        awj: 3
    }
};
const {abc, xyz, qnc: {awj}} = obj;
console.log(abc, xyz, awj); // 1, 2, 3
```js

-- T.J. Crowder


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

Re: Extend Object Dereferencing

T.J. Crowder-2
In reply to this post by T.J. Crowder-2
On Wed, Oct 25, 2017 at 1:15 PM, Sebastian Malton
>
> I didn't mean that part when I said multiple levels. I meant
> the following 
> ```
> const abc = { { bcd, cde, efg: {qnc} } = obj};
>
> ```

I was responding to your initial assertion in your first message, which didn't show that form. It just said

> Currently you can do the following 
>
> `const {abc, xyz, qnc} = obj;`
>
> However if you want to go more than one level deep then you have to do it again for each level.
>
> ...
>
> I therefore propose the ability to do the following 
>
> `const {abc, xyz, qnc.awj} = obj;`
>
> And this would create the variables 'abc'', 'xyz', 'awj' with the
> values from obj.

I was pointing out how you can do that without having to "do it again."

Perhaps you could clarify what you're actually proposing. Your first message talked about individual variables for the parts of `obj`; subsequently you seemed to be talking about creating a new object (combining object initializers with destructuring)...?

-- T.J. Crowder


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