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

Get property itself not value of property #83

Open
bhaidar opened this issue Apr 23, 2017 · 5 comments
Open

Get property itself not value of property #83

bhaidar opened this issue Apr 23, 2017 · 5 comments

Comments

@bhaidar
Copy link

bhaidar commented Apr 23, 2017

Hi,
Can I use this utility to get the property itself rather than value?

For instance, I have a string path to a property, I want to delete that property from an object, so using this utility to find the property by string path, I get only its value and not a reference to it to use it later.

Any idea?
Thanks

@revelt
Copy link

revelt commented May 4, 2017

But you can use objectPath.del — it is the same thing, isn't it?

@CavalcanteLeo
Copy link

Im facing the same issue, but i don't want to delete, just to store the reference path in another object

@revelt
Copy link

revelt commented Sep 23, 2020

@CavalcanteLeo My package ast-monkey-traverse does the traversal of objects and arrays; it does report object-path-compatible paths which can be used in object-path's methods like get() etc. In example below, I extract all paths in object-path notation of keys named foo, getting ["a.foo", "a.foo.bar.foo", "a.foo.d.e.foo"]:

// Extract paths of all object keys named "foo"

import { strict as assert } from "assert";
import op from "object-path";
import traverse from "ast-monkey-traverse";

const paths = [];
const source = {
  a: {
    foo: {
      bar: {
        foo: "c",
      },
      d: {
        e: {
          foo: "f",
        },
      },
    },
  },
};

traverse(source, (key, val, innerObj) => {
  // if currently an object is traversed, you get both "key" and "val"
  // if it's array, only "key" is present, "val" is undefined
  const current = val !== undefined ? val : key;
  if (
    // it's object (not array)
    val !== undefined &&
    // and has the key we need
    key === "foo"
  ) {
    // push the path to array in the outer scope
    paths.push(innerObj.path);
  }
  return current;
});
assert.deepEqual(paths, ["a.foo", "a.foo.bar.foo", "a.foo.d.e.foo"]);

// then process the paths in `object-path`:
// paths.forEach(path => { op.get(source, path) }) ... or something similar

Going further, globby could be used to filter the key names and so on...

The object-path could be extended with more methods but smaller size of the API is a virtue :)

@CavalcanteLeo
Copy link

I will look into it, thanks in advance

@PAEz
Copy link

PAEz commented Nov 1, 2020

Wouldnt this just be?.....

    let obj={a:{b:1}};
    let path = "a.b";
    let pos = path.lastIndexOf('.');
    let prop = pos ? path.substr(pos + 1) : path;
    let parentPath = pos ? path.substr(0, pos) : "";
    let parent = pos ? objectPath.get(obj, parentPath) : obj;

parent[prop]

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

4 participants