Skip to content

Instantly share code, notes, and snippets.

@dhoko
Last active August 29, 2015 13:56
Show Gist options
  • Save dhoko/9231111 to your computer and use it in GitHub Desktop.
Save dhoko/9231111 to your computer and use it in GitHub Desktop.
Get value from an object from a path inside the object
/**
* Get a value from an object by its path
* @param {Object} obj
* @param {String} path Path to the value key1.key2.key3...
* @return {Object} Value
*/
function valuePathObject(obj, path) {
path.split(".").forEach(function(item){
obj = obj[item]
});
return obj;
}
@dhoko
Copy link
Author

dhoko commented Feb 26, 2014

Exemple :

var toro = {
  description : {
    fr_fr : {
      size : 40,
      name: "toto"
    }
  }
}

var selector = "description.fr_fr.size";

console.log(valueRecursive(toro,selector));
// 40

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment