-
Notifications
You must be signed in to change notification settings - Fork 84
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
Comments
But you can use |
Im facing the same issue, but i don't want to delete, just to store the reference path in another object |
@CavalcanteLeo My package // 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, The |
I will look into it, thanks in advance |
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] |
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
The text was updated successfully, but these errors were encountered: