Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript: Get deep value from object by passing path to it as string [duplicate]

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!

like image 626
7elephant Avatar asked Jan 11 '12 10:01

7elephant


5 Answers

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.

like image 127
Tadeck Avatar answered Oct 03 '22 09:10

Tadeck


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'))
like image 20
qiao Avatar answered Oct 03 '22 11:10

qiao


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("."));
}

See http://jsfiddle.net/kExSr/

like image 23
fyr Avatar answered Oct 03 '22 10:10

fyr


something like:

function(obj, path) {
  var current=obj; 
  path.split('.').forEach(function(p){ current = current[p]; }); 
  return current;
}
like image 22
Dan D. Avatar answered Oct 03 '22 11:10

Dan D.


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;
}

Live example

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"];
like image 27
T.J. Crowder Avatar answered Oct 03 '22 09:10

T.J. Crowder