Javascript – retrieve object property path [duplicate]

Here’s a way:

function getKey(key, obj) {
  return key.split('.').reduce(function(a,b){
    return a && a[b];
  }, obj);
}

getKey('foo.bar', obj); //=> "I want this"

Leave a Comment