findPath

Searches thru an object and returns the path to needle (or returns false if needle is not found). var foo = [ {bar:"baz"}, {bar:"qux"} ]; var needle = "qux"; result = [1,"bar"] thus: foo[ result[0] ][ result[1] ] will get you to needle.

JavaScript

function findPath(haystack, needle) {
    function lookDeeper(haystack, needle, path) {
        if (typeof haystack !== 'object') return false;
        if ( $.isArray(haystack) ) {
            for (var i = haystack.length - 1; i >= 0; i--) {
                var val = haystack[i];
                var currentPath = $.merge([], path);
                currentPath.push(i);

                if (val === needle) return currentPath;
                var foundPath = lookDeeper(val, needle, currentPath);
                if (foundPath) return foundPath;
            }
        }
        for (var prop in haystack) {
            var val = haystack[prop];
            var currentPath = $.merge([], path);
            if ( prop === needle ) return currentPath;
            currentPath.push(prop);
            
            if (val === needle) return currentPath;
            var foundPath = lookDeeper(val, needle, currentPath);
            if (foundPath) return foundPath;
        }
    }
    return lookDeeper(haystack, needle, []);
}

var haystack = [ {foo: "bar"}, {baz: "qux"} ];
var needle = "foo";

var r = findPath( haystack, needle );

console.log(r);
console.log( haystack[ r[0] ] );