Reputation: 1995
I'm trying to create a function that "resolves" the dot delimited path of a nested property in an object.
So say you have the following object:
var obj = {
A: {
A_1: {
},
A_2: {
A_2_a: {},
A_2_b: {
A_2_b_1: {},
A_2_b_2: {},
}
},
A_3: {},
},
B: {
B_1: {
},
B_2: {
B_2_a: {
},
B_2_b: {
B_2_b_1: {},
B_2_b_2: {},
}
},
B_3: {},
},
};
I want to call a function on the object like resolve(obj, "B_2_b_2")
and have it return the full object path to the property, ie: B.B_2.B_2_b.B_2_b_2
.
Upvotes: 1
Views: 525
Reputation: 1995
Eureka! I figured it out! The below answer is in the form of a library I'm writing but it should be relatively easy to follow.
As it turns out, the best course of action (as far as I can tell) is to first using a separate function, build an object from the target object containing all the property paths:
/**
* Returns an object containing all of the property paths of an object. Each
* property path is referenced by the property name.
* @param {object} The object to target
* @return {object} Object containing paths ELSE undefined
*/
paths: function( obj, path, lastKey, nextKey ) {
var o, key,
path = path ? path : {},
lastKey = lastKey ? lastKey : "",
nextKey = nextKey ? nextKey : "";
for ( o in obj ) {
// Push path onto stack
path[o] = (nextKey + "." + lastKey + "." + o).replace(/^[.]+/g, "");
// Pass updated "nextKey" along with next recurse
key = nextKey + "." + lastKey;
// Call again on all nested objects
if ( (lib).isPlainObject(obj[o]) ) {
(lib).paths(obj[o], path, o, key);
}
}
return (lib).len(path) ? path : undefined;
},
Then we use the resolve method as a "wrapper" to the paths method, returning the targeted property key's namespace.
resolve: function( obj, key ) {
return (lib).paths(obj)[key];
},
Using the object I posted originally above:
var res = o.resolve(obj, "A_2_b_1");
// Returns "A.A_2.A_2_b.A_2_b_1"
Just for reference, the paths
method returns an object that looks something like this:
// {
// A: [...]
// A_1: [...]
// A_2: [...]
// A_2_a: [...]
// A_2_b: [...]
// A_2_b_1: [
// 0: "A_2_b_1"
// 1: "A.A_2.A_2_b.A_2_b_1"
// ]
// A_2_b_2: [...]
// A_2_c: [...]
// A_3: [...]
// B: [...]
// ...
// }
Where each property maps to its path in the object.
Upvotes: 1
Reputation: 665185
Assuming a naming convention, like in your example object:
function resolve(id) {
var parts = id.split("_");
var path = [];
for (var i=0; i<parts.length; i++)
path.push(parts.slice(0, i+1).join("_"));
return path;
}
> resolve("B_2_b_2")
["B", "B_2", "B_2_b", "B_2_b_2"]
> resolve("B_2_b_2").join(".")
"B.B_2.B_2_b.B_2_b_2"
With the path array you can easily recurse over your nested object to get the property values.
A tree search in a data object is trivial. However, we can optimize that by assuming a naming convention:
function resolve(obj, id) {
if (id in obj)
return [id]; // we've found it
var path;
for (var l=id.length-1; l>0; l--) {
var sub = id.substr(0, l);
if (sub in obj && (path = resolve(obj[sub], id))) {
path.unshift(sub);
return path;
}
}
for (var prop in obj) {
if (path = resolve(obj[prop], id)) {
path.unshift(prop);
return path;
}
}
return null;
}
Upvotes: 1