Javascript: Get deep value from object by passing path to it as string
JavascriptSyntaxPathJavascript Problem Overview
> Possible Duplicate:
> Accessing nested JavaScript objects with string key
Maybe the title is not clear enough, I just didn't know how to specify what I'm looking for and my English is really bad, sorry.
I'm trying to create function that returns object value, but also plays nice with nested objects. For example:
var obj = {
foo: { bar: 'baz' }
};
I want to access the value of obj.foo.bar by suppling the string "foo.bar" to the function.
function(obj, path) {
// Path can be "foo.bar", or just "foo".
}
Thanks!
Javascript Solutions
Solution 1 - Javascript
This works correctly:
var deep_value = function(obj, path){
for (var i=0, path=path.split('.'), len=path.length; i<len; i++){
obj = obj[path[i]];
};
return obj;
};
Here is the proof / demo: jsfiddle.net/tadeck/5Pt2q/13/
EDIT: I have removed redundant variables, shortened the code.
Solution 2 - Javascript
Consider this:
var obj = {
foo: { bar: 'baz' }
};
function deepFind(obj, path) {
var paths = path.split('.')
, current = obj
, i;
for (i = 0; i < paths.length; ++i) {
if (current[paths[i]] == undefined) {
return undefined;
} else {
current = current[paths[i]];
}
}
return current;
}
console.log(deepFind(obj, 'foo.bar'))
Solution 3 - Javascript
You mean something like this ? It is a recursive version
function recLookup(obj, path) {
parts = path.split(".");
if (parts.length==1){
return obj[parts[0]];
}
return recLookup(obj[parts[0]], parts.slice(1).join("."));
}
Solution 4 - Javascript
something like:
function(obj, path) {
var current=obj;
path.split('.').forEach(function(p){ current = current[p]; });
return current;
}
Solution 5 - Javascript
You'd want to split the string on the dot and then repeatedly index into the object, e.g. along the lines of:
function goDeep(obj, path) {
var parts = path.split('.'),
rv,
index;
for (rv = obj, index = 0; rv && index < parts.length; ++index) {
rv = rv[parts[index]];
}
return rv;
}
That works because you can access the property of an object in a couple of different ways: There's dotted syntax using a literal (obj.foo
), and there's bracketed syntax using a string (obj["foo"]
). In the latter case, the string can be the result of any expression, it doesn't have to be a string literal. In in all of the, rv
is set to the same value:
rv = obj.foo.bar;
// Or
rv = obj.foo["bar"];
// Or
f = "foo";
rv = obj[f].bar;
// Or
s = "b";
rv = obj.foo[s + "ar"];