1

I can't find how to do this... ?(Finding a variable in a block var [javascript] !)? I'd like to be able to find lets say Nicholas Phillips from this...

var profiles = {
    Nicholas_Phillips: {
        Name: "Nicholas_Phillips"
    }
}

and get in return the variable part "profiles.Nicholas_Phillips" or the parent holder I guess?!

Nick
  • 13
  • 1
  • 5
  • 1
    Recursively search your object. – Sirko Dec 17 '14 at 14:57
  • @Sirko Can you please give me an example? I'm kinda new-ish to JavaScript... – Nick Dec 17 '14 at 14:57
  • Take a look at http://stackoverflow.com/questions/1946165/json-find-in-javascript and http://stackoverflow.com/questions/19253753/javascript-find-json-value – Christian Benseler Dec 17 '14 at 15:03
  • 1
    You may need to post more of the code in context. All you've posted so far is the search result. – Lee Jenkins Dec 17 '14 at 15:04
  • The path you've given at the end of your question is wrong. The fullpath to the property with the matching value is `profiles.Nicholas_Phillips.Name`, not `profiles.Nicholas_Phillips`. – T.J. Crowder Dec 17 '14 at 15:13

3 Answers3

1

You'll need a recursive search, something vaguely like:

var profiles = {
  Nicholas_Phillips: {
    Name: "Nicholas_Phillips"
  }
};
function find(obj, value, path) {
  if (Object.keys(obj).some(function(key) {
      var thisValue = obj[key];
      var p;
      if (thisValue === value) {
        // Found it, set the path and return true to stop looping (true = stops `some`)
        path = path ? path + "." + key : key;
        return true;
      }
      if (typeof thisValue === "object") {
        // Found an object, recurse into it
        p = find(thisValue, value, path ? path + "." + key : key);
        if (p) {
          // We found it (somewhere) in that object, set the path and stop looping
          path = p;
          return true;
        }
      }
    })) {
    // We found it, return path
    return path;
  }
  // Not found, return undefined
  return undefined;
}
snippet.log(find(profiles, "Nicholas_Phillips")); // "Nicholas_Phillips.Name"
snippet.log(find(profiles, "foo")); // undefined
<!-- Script provides the `snippet` object, see http://meta.stackexchange.com/a/242144/134069 -->
<script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script>

That implementation of find returns the path to the property it found. It is not extensively tested, but it should point you the right way.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
0

You simply have to search your object recursively:

function search( obj, needle, path ) {

  // get all keys from current object
  var keys = Object.keys( obj );

  // walk all properties
  for( var i=0; i<keys.length; i++ ) {

    // check for a match
    if( obj[ keys[i] ] === needle ) {
       return path + '.' + keys[i];
    }

    // if it is a subject, search it recursively
    if( typeof obj[ keys[i] ] == 'object' ) {
      var recCall = search( obj[ keys[i] ], needle, path + '.' + keys[i] );

      // if we had a match, return it
      if( recCall !== null ) {
        return recCall;
      }
    }
  }

  // if we came this far, no match was found
  return null;
}

// execute
var path = search( profiles, "Nicholas_Phillips", 'profiles' );

I'm not sure, what exactly you want to search for (value or a key?) and what to return (path or reference to parent?), but the above code should work for all with some minor changes.

Sirko
  • 72,589
  • 19
  • 149
  • 183
  • Its pretty close but not close enough... I'm looking for a way to find this profiles.Nicholas_Phillips.Name = "Nicholas Phillips" - And it'll output profiles.Nicholas_Phillips by searching Name = "Nicholas Phillips"! – Nick Dec 17 '14 at 15:14
  • @Nick: Then adjust it to do what you want. There's more than enough here to get you going. If you only want to search `Name` properties, and leave the `Name` part off the path, you can do that. – T.J. Crowder Dec 17 '14 at 15:21
-1

Here's a quick function that takes the object you want to search,the child object we want to search, and the value we want from that child. It will then look at all the keys in the object and see if they match your value. If one does, it also makes sure that that specific object (not its prototype) has the key. Then it checks if the child object has the desired value, and returns that value.

   function findKey(obj, child, value){
       for (key in obj) {
         if (key === child && obj.hasOwnProperty(child)){
             if(obj[child][value]){
                     return obj[child][value]
                  }
          }
      }
    }

So if we ran this in the context of your question:

findKey(profiles, "Nicholas_Phillips", "Name");

It would return

"Nicholas_Phillips"

Otherwise it will return undefined

Aweary
  • 2,302
  • 17
  • 26