javascript - Get object key path by value -


i'm trying path of property based on value put function. i've searched high , low, , reverse acheivable (_.pluck). work this:

given

var myobj = {   some: {     thing: 'hello'   },   other: {     thing: 'world'   } } 

getpath(myobj, 'hello'); return 'some.thing' or ['some', 'thing'] (array preferably).

would awesome if flexible different sized objects too. +points lodash solution.

this works 2 levels deep, quick solution:

function getpath (obj, name) {     var location = null;      (var lvl_1 in obj) {         if (obj[lvl_1] === name) {             location = lvl_1;             return location;         } else if (typeof obj[lvl_1] === 'object') {             (var lvl_2 in obj[lvl_1]) {                 if (obj[lvl_1][lvl_2] === name) {                     location = lvl_1 + '.' + lv;_2;                     return location;                 }             }         }     }      return location; } 

i ran variables

> getpath(myobj, 'hello')

"some.thing"

if want array, split it:

> getpath(myobj, 'hello').split('.')

["some", "thing"]