Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding ability for deep object property lookup (_.get) #2370

Closed
patrickml opened this issue Dec 2, 2015 · 2 comments
Closed

Adding ability for deep object property lookup (_.get) #2370

patrickml opened this issue Dec 2, 2015 · 2 comments

Comments

@patrickml
Copy link

This is simular to that of lodash and I believe there is a big use for this functionality to be added to the core. One of the biggest benefits of this is that you no longer have to check for undefined up the object chain like so

Before this.object && this.object.one && this.object.one.two
Now _.get(this.object, 'one.two')

I found most of this on another post I cannot remember who did it but this is very useful

_.mixin({
  get: function (obj, key) {
      var type = typeof key;

    if (type == 'string' || type == "number") {
      key = ("" + key).replace(/\[(.*?)\]/,/\[(.*?)\]/, function (m, key) { //handle case where [1] may occur
        return '.' + key.replace(/["']/g,/["']/g, ""); //strip quotes
      }).split('.');
    }
    for (var i = 0, l = key.length; i < l; i++) {
      if (typeof obj !== 'undefined' && _.has(obj, key[i])) obj = obj[key[i]];
      else return undefined;
    }
    return obj;
  }
});
@akre54
Copy link
Collaborator

akre54 commented Dec 2, 2015

This has been proposed and rejected a few times (search for "string programming" or "deep path" and you'll find #2268, #1169, #1266 and others). Also check out underscore-contrib.

@akre54 akre54 closed this as completed Dec 2, 2015
captbaritone added a commit to captbaritone/underscore that referenced this issue Mar 30, 2016
Inspired by underscore-contrib's `_.getPath` and lodash's deep property access,
this adds deep property access array shorthand it many aspects of Underscore,
including:

* `_.property`
* `_.iteratee` and all methods that depend on it.

It also exposes a convenience function `_.get` for accessing deep properties
with a default.

Possible Concerns
-----------------

While `_.property` does not get called in any place that would present
a performance concern, `getLength` (which is created by `_.property`) gets used
in many places which _may_ have performance ramifications.

I've placed the `isArray` call in the outer function of `_.property` in an
attempt to reduce complexity inside the generated function. This required
defining `isArray` earlier.

It's possible that this still isn't fast enough, and we should just define
`getLength` manually.

Further work
------------

The following methods should probably also accept the array syntax:

* `_.result`
* `_.has`

The following companion methods should be created:

* `_.set`

Fixes: jashkenas#2372, an elegant solution to an oft requested feature: (see: jashkenas#2370,
 jashkenas#2268, jashkenas#1169, jashkenas#1266)
captbaritone added a commit to captbaritone/underscore that referenced this issue Mar 30, 2016
Inspired by underscore-contrib`'s _.getPath` and lodash's deep property access,
this adds an Array deep property access shorthand to many aspects of
Underscore, including:

* `_.property`
* `_.iteratee` and all methods that depend on it.

It also exposes a convenience function `_.get` for accessing deep properties
with a default.

Possible Concerns
-----------------

While `_.property` does not get called in any place that would present
a performance concern, `getLength` (which is created by `_.property`) gets used
in many places which _may_ have performance ramifications.

I've placed the `isArray` call in the outer function of `_.property` in an
attempt to reduce complexity inside the generated function. This required
defining `isArray` earlier.

It's possible that this still isn't fast enough, and we should just define
`getLength` manually.

Further work
------------

The following methods should probably also accept the array syntax:

* `_.result`
* `_.has`

The following companion methods should be created:

* `_.set`

Fixes: jashkenas#2372, an elegant solution to an oft requested feature: (see: jashkenas#2370,
 jashkenas#2268, jashkenas#1169, jashkenas#1266)
captbaritone added a commit to captbaritone/underscore that referenced this issue Apr 7, 2016
Inspired by underscore-contrib`'s _.getPath` and lodash's deep property access,
this adds an Array deep property access shorthand to many aspects of
Underscore, including:

* `_.property`
* `_.iteratee` and all methods that depend on it.
* `_.has`
* `_.result` (including deep evaluation)

Possible Concerns
=================

`_.property`
------------

While `_.property` does not get called in any place that would present
a performance concern, `getLength` (which is created by `_.property`) gets used
in many places which _may_ have performance ramifications.

I've placed the `isArray` call in the outer function of `_.property` in an
attempt to reduce complexity inside the generated function. This required
defining `isArray` earlier.

It's possible that this still isn't fast enough, and we should just define
`getLength` manually.

`_.has`
-------

This adds some complexity to `_.has` which underpins many methods. It's
possible that we may want to use a simpler "flat" version of `_.has`
internally.

Further work
============

We may want to expose some additional helper methods:

* `_.get`
* `_.set`
* `_.unset`

Fixes: jashkenas#2372, an elegant solution to an oft requested feature: (see: jashkenas#2370,
 jashkenas#2268, jashkenas#1169, jashkenas#1266)
captbaritone added a commit to captbaritone/underscore that referenced this issue Oct 1, 2016
Inspired by underscore-contrib`'s _.getPath` and lodash's deep property access,
this adds an Array deep property access shorthand to many aspects of
Underscore, including:

* `_.property`
* `_.iteratee` and all methods that depend on it.
* `_.has`
* `_.result` (including deep evaluation)

Possible Concerns
=================

`_.property`
------------

While `_.property` does not get called in any place that would present
a performance concern, `getLength` (which is created by `_.property`) gets used
in many places which _may_ have performance ramifications.

I've placed the `isArray` call in the outer function of `_.property` in an
attempt to reduce complexity inside the generated function. This required
defining `isArray` earlier.

It's possible that this still isn't fast enough, and we should just define
`getLength` manually.

`_.has`
-------

This adds some complexity to `_.has` which underpins many methods. It's
possible that we may want to use a simpler "flat" version of `_.has`
internally.

Further work
============

We may want to expose some additional helper methods:

* `_.get`
* `_.set`
* `_.unset`

Fixes: jashkenas#2372, an elegant solution to an oft requested feature: (see: jashkenas#2370,
 jashkenas#2268, jashkenas#1169, jashkenas#1266)
captbaritone added a commit to captbaritone/underscore that referenced this issue Oct 2, 2016
Inspired by underscore-contrib`'s _.getPath` and lodash's deep property access,
this adds an Array deep property access shorthand to many aspects of
Underscore, including:

* `_.property`
* `_.iteratee` and all methods that depend on it.
* `_.has`
* `_.result` (including deep evaluation)

Possible Concerns
=================

`_.property`
------------

While `_.property` does not get called in any place that would present
a performance concern, `getLength` (which is created by `_.property`) gets used
in many places which _may_ have performance ramifications.

I've placed the `isArray` call in the outer function of `_.property` in an
attempt to reduce complexity inside the generated function. This required
defining `isArray` earlier.

It's possible that this still isn't fast enough, and we should just define
`getLength` manually.

`_.has`
-------

This adds some complexity to `_.has` which underpins many methods. It's
possible that we may want to use a simpler "flat" version of `_.has`
internally.

Further work
============

We may want to expose some additional helper methods:

* `_.get`
* `_.set`
* `_.unset`

Fixes: jashkenas#2372, an elegant solution to an oft requested feature: (see: jashkenas#2370,
 jashkenas#2268, jashkenas#1169, jashkenas#1266)
captbaritone added a commit to captbaritone/underscore that referenced this issue Oct 8, 2016
Inspired by underscore-contrib`'s _.getPath` and lodash's deep property access,
this adds an Array deep property access shorthand to many aspects of
Underscore, including:

* `_.property`
* `_.iteratee` and all methods that depend on it.
* `_.has`
* `_.result` (including deep evaluation)

Possible Concerns
=================

`_.property`
------------

While `_.property` does not get called in any place that would present
a performance concern, `getLength` (which is created by `_.property`) gets used
in many places which _may_ have performance ramifications.

I've placed the `isArray` call in the outer function of `_.property` in an
attempt to reduce complexity inside the generated function. This required
defining `isArray` earlier.

It's possible that this still isn't fast enough, and we should just define
`getLength` manually.

`_.has`
-------

This adds some complexity to `_.has` which underpins many methods. It's
possible that we may want to use a simpler "flat" version of `_.has`
internally.

Further work
============

We may want to expose some additional helper methods:

* `_.get`
* `_.set`
* `_.unset`

Fixes: jashkenas#2372, an elegant solution to an oft requested feature: (see: jashkenas#2370,
 jashkenas#2268, jashkenas#1169, jashkenas#1266)
captbaritone added a commit to captbaritone/underscore that referenced this issue Oct 8, 2016
Inspired by underscore-contrib`'s _.getPath` and lodash's deep property access,
this adds an Array deep property access shorthand to many aspects of
Underscore, including:

* `_.property`
* `_.iteratee` and all methods that depend on it.
* `_.has`
* `_.result` (including deep evaluation)

Possible Concerns
=================

`_.property`
------------

While `_.property` does not get called in any place that would present
a performance concern, `getLength` (which is created by `_.property`) gets used
in many places which _may_ have performance ramifications.

I've placed the `isArray` call in the outer function of `_.property` in an
attempt to reduce complexity inside the generated function. This required
defining `isArray` earlier.

It's possible that this still isn't fast enough, and we should just define
`getLength` manually.

`_.has`
-------

This adds some complexity to `_.has` which underpins many methods. It's
possible that we may want to use a simpler "flat" version of `_.has`
internally.

Further work
============

We may want to expose some additional helper methods:

* `_.get`
* `_.set`
* `_.unset`

Fixes: jashkenas#2372, an elegant solution to an oft requested feature: (see: jashkenas#2370,
 jashkenas#2268, jashkenas#1169, jashkenas#1266)
@NarHakobyan
Copy link

use underscore.get module

const {get} = require('underscore.get');

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants