Proposal: Object.path([ ... ], defaultValue)

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

Proposal: Object.path([ ... ], defaultValue)

Vikash Agrawal
Hello Everyone, 

It is a lot of times that we see `x is not a property of undefined` and to solve this we have to resort to &&, something like -> data && data.childData && data.childData.innerDataArray && data.childData.innerDataArray.length > 0 whiich is quiet verbose. 

I suggest, we should add a utility method like Object.path, which takes an array of strings and returns the value from a given nested object. Also, this can take a second argument which will be a default value if not found. 

This is inspired by path and pathOr as implemented in http://ramdajs.com/docs/#path

Regards
~Vikash

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

Re: Proposal: Object.path([ ... ], defaultValue)

Sebastian Malton
This would be redundant because of the `?.` operator once it gets merged

Sebastian Malton

Sent: February 15, 2018 5:27 PM
Subject: Proposal: Object.path([ ... ], defaultValue)

Hello Everyone, 

It is a lot of times that we see `x is not a property of undefined` and to solve this we have to resort to &&, something like -> data && data.childData && data.childData.innerDataArray && data.childData.innerDataArray.length > 0 whiich is quiet verbose. 

I suggest, we should add a utility method like Object.path, which takes an array of strings and returns the value from a given nested object. Also, this can take a second argument which will be a default value if not found. 

This is inspired by path and pathOr as implemented in http://ramdajs.com/docs/#path

Regards
~Vikash

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

Re: Proposal: Object.path([ ... ], defaultValue)

Vikash Agrawal
In reply to this post by Vikash Agrawal
Sure Sebastian. 

On Fri, 16 Feb 2018 at 3:59 AM, Sebastian Malton <[hidden email]> wrote:
This would be redundant because of the `?.` operator once it gets merged

Sebastian Malton

Sent: February 15, 2018 5:27 PM
Subject: Proposal: Object.path([ ... ], defaultValue)

Hello Everyone, 

It is a lot of times that we see `x is not a property of undefined` and to solve this we have to resort to &&, something like -> data && data.childData && data.childData.innerDataArray && data.childData.innerDataArray.length > 0 whiich is quiet verbose. 

I suggest, we should add a utility method like Object.path, which takes an array of strings and returns the value from a given nested object. Also, this can take a second argument which will be a default value if not found. 

This is inspired by path and pathOr as implemented in http://ramdajs.com/docs/#path

Regards
~Vikash

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

Re: Proposal: Object.path([ ... ], defaultValue)

Andrea Giammarchi-2
In reply to this post by Sebastian Malton
?. does not *compose* though, and a standard way to retrieve a path is needed in JSON Pointer too [1]

I've personally used similar approach in various cases, and old libraries like Ext.JS used to
`Function("this." + path.join('.')).call(target)` to retrieve a path in a speedy way.

Beside the "array of string" bit, which is limited if we think nested Arrays and/or Symbols as properties, I think a standard way to retrieve a path would be beneficial in various cases.

Just my 2 cents.

On Thu, Feb 15, 2018 at 11:29 PM, Sebastian Malton <[hidden email]> wrote:
This would be redundant because of the `?.` operator once it gets merged

Sebastian Malton

Sent: February 15, 2018 5:27 PM
Subject: Proposal: Object.path([ ... ], defaultValue)

Hello Everyone, 

It is a lot of times that we see `x is not a property of undefined` and to solve this we have to resort to &&, something like -> data && data.childData && data.childData.innerDataArray && data.childData.innerDataArray.length > 0 whiich is quiet verbose. 

I suggest, we should add a utility method like Object.path, which takes an array of strings and returns the value from a given nested object. Also, this can take a second argument which will be a default value if not found. 

This is inspired by path and pathOr as implemented in http://ramdajs.com/docs/#path

Regards
~Vikash

_______________________________________________
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: Object.path([ ... ], defaultValue)

Jordan Harband
Seems like it composes fine (using `??.`/`??[` syntax): `function retrieve(obj, path) { return path.split('.').reduce((prev, key) => prev??[key], obj); }   retrieve(someObject, 'a.b.c.d')`

On Thu, Feb 15, 2018 at 2:47 PM, Andrea Giammarchi <[hidden email]> wrote:
?. does not *compose* though, and a standard way to retrieve a path is needed in JSON Pointer too [1]

I've personally used similar approach in various cases, and old libraries like Ext.JS used to
`Function("this." + path.join('.')).call(target)` to retrieve a path in a speedy way.

Beside the "array of string" bit, which is limited if we think nested Arrays and/or Symbols as properties, I think a standard way to retrieve a path would be beneficial in various cases.

Just my 2 cents.

On Thu, Feb 15, 2018 at 11:29 PM, Sebastian Malton <[hidden email]> wrote:
This would be redundant because of the `?.` operator once it gets merged

Sebastian Malton

Sent: February 15, 2018 5:27 PM
Subject: Proposal: Object.path([ ... ], defaultValue)

Hello Everyone, 

It is a lot of times that we see `x is not a property of undefined` and to solve this we have to resort to &&, something like -> data && data.childData && data.childData.innerDataArray && data.childData.innerDataArray.length > 0 whiich is quiet verbose. 

I suggest, we should add a utility method like Object.path, which takes an array of strings and returns the value from a given nested object. Also, this can take a second argument which will be a default value if not found. 

This is inspired by path and pathOr as implemented in http://ramdajs.com/docs/#path

Regards
~Vikash

_______________________________________________
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: Object.path([ ... ], defaultValue)

Naveen Chawla
Of course you can compose paths with it (and even without it), but it doesn't compose on it's own like a standard path syntax could.

On Sun, 18 Feb 2018, 9:14 am Jordan Harband, <[hidden email]> wrote:
Seems like it composes fine (using `??.`/`??[` syntax): `function retrieve(obj, path) { return path.split('.').reduce((prev, key) => prev??[key], obj); }   retrieve(someObject, 'a.b.c.d')`

On Thu, Feb 15, 2018 at 2:47 PM, Andrea Giammarchi <[hidden email]> wrote:
?. does not *compose* though, and a standard way to retrieve a path is needed in JSON Pointer too [1]

I've personally used similar approach in various cases, and old libraries like Ext.JS used to
`Function("this." + path.join('.')).call(target)` to retrieve a path in a speedy way.

Beside the "array of string" bit, which is limited if we think nested Arrays and/or Symbols as properties, I think a standard way to retrieve a path would be beneficial in various cases.

Just my 2 cents.

On Thu, Feb 15, 2018 at 11:29 PM, Sebastian Malton <[hidden email]> wrote:
This would be redundant because of the `?.` operator once it gets merged

Sebastian Malton

Sent: February 15, 2018 5:27 PM
Subject: Proposal: Object.path([ ... ], defaultValue)

Hello Everyone, 

It is a lot of times that we see `x is not a property of undefined` and to solve this we have to resort to &&, something like -> data && data.childData && data.childData.innerDataArray && data.childData.innerDataArray.length > 0 whiich is quiet verbose. 

I suggest, we should add a utility method like Object.path, which takes an array of strings and returns the value from a given nested object. Also, this can take a second argument which will be a default value if not found. 

This is inspired by path and pathOr as implemented in http://ramdajs.com/docs/#path

Regards
~Vikash

_______________________________________________
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