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

Add support for arrays #12

Open
ragulka opened this issue Jan 20, 2014 · 5 comments
Open

Add support for arrays #12

ragulka opened this issue Jan 20, 2014 · 5 comments

Comments

@ragulka
Copy link

ragulka commented Jan 20, 2014

It'd be great if dotty could work with nested arrays as well, for example, with something like this:

{
  some: 'property',
  other: [{ properties: true }, { properties: true }]
}
@jgrund
Copy link

jgrund commented Jun 21, 2014

What about:

  var dotty = require('dotty');

  var obj = {
    some: 'property',
    other: [{ properties: true }, { properties: true }]
  };

  dotty.get(obj, 'other.0'); // { properties: true }
  dotty.get(obj, 'other.1'); // { properties: true }

The syntax is a little awkward, but seems to work.

@kbariotis
Copy link

Since the goal of this project is to access specific properties I would suggest this feature to return all objects from the array, but only with the specified fields.

 var dotty = require('dotty');

  var obj = {
    some: 'property',
    other: [
      { 
        property1: true, 
        property2: false 
      }, 
      { 
        property1: true, 
        property2: false 
      }
    ]
  };

  dotty.get(obj, 'other.property1'); // [{ property1: true }, { property1: true }]

@deoxxa
Copy link
Owner

deoxxa commented Mar 16, 2016

The actual original goal of this project was to replace all the places where I had code like this:

var something = null;
try {
  something = a.b.c.d.e;
} catch (e) { something = 'default'; }

x.y = x.y || {};
x.y.z = something;

with this kind of thing:

dotty.put(x, 'y.z', dotty.get(a, 'b.c.d.e') || 'default');

Clearly it does more now than it did then, but this is a pretty major departure from the existing API. It might be better implemented as a different method, or even as another module that perhaps builds atop this?

@kbariotis
Copy link

Something I have in mind and wrote the above comment is to replicate the MongoDB field selection on an object.

So with an object like this:

var object = {
  "name": {
    "first_name": "Kostas",
    "last_name": "Bariotis"
  }
}

I could easily create an exact same object that only contains the first_name attribute.

Do you think dotty would be a nice fit for this kind of job?

Thanks @deoxxa :)

@brentchow
Copy link

+1

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

No branches or pull requests

5 participants