Skip to content

Instantly share code, notes, and snippets.

@diegocard
Last active August 29, 2015 14:22
Show Gist options
  • Save diegocard/e18c8c4632b1488b688a to your computer and use it in GitHub Desktop.
Save diegocard/e18c8c4632b1488b688a to your computer and use it in GitHub Desktop.

Revisions

  1. diegocard revised this gist Jun 7, 2015. 1 changed file with 19 additions and 19 deletions.
    38 changes: 19 additions & 19 deletions keyLookup.js
    Original file line number Diff line number Diff line change
    @@ -21,25 +21,25 @@ function keyLookup(obj, string) {
    // Test:

    var testObj = {
    someStuff: 1,
    someObjectStuff: {
    arr: [1, 2, 3]
    },
    a: {
    b: {
    func : function() {
    return 1;
    },
    c: [
    {
    test : {
    f : 1
    }
    },
    1
    ]
    }
    }
    someStuff: 1,
    someObjectStuff: {
    arr: [1, 2, 3]
    },
    a: {
    b: {
    func : function() {
    return 1;
    },
    c: [
    {
    test : {
    f : 1
    }
    },
    1
    ]
    }
    }
    }

    keyLookup(testObj, "test") // returns "a : b : c : 0 : test"
  2. diegocard revised this gist Jun 7, 2015. 1 changed file with 44 additions and 18 deletions.
    62 changes: 44 additions & 18 deletions keyLookup.js
    Original file line number Diff line number Diff line change
    @@ -1,19 +1,45 @@
    keyLookup: function(obj, string) {
    if (_.isObject(obj)) {
    var path = null;
    _.find(obj, function(value, key) {
    if (_.isString(key) && key.includes(string)) {
    path = key.toString();
    return true;
    } else if (_.isObject(value) && !_.isFunction(value)) {
    var foundKeyValueRec = keyLookup(value, string);
    // Call recursively
    if (foundKeyValueRec) {
    path = key + ' : ' + foundKeyValueRec;
    return true;
    }
    }
    });
    return path;
    function keyLookup(obj, string) {
    if (_.isObject(obj)) {
    var path = null;
    _.find(obj, function(value, key) {
    if (_.isString(key) && key.includes(string)) {
    path = key.toString();
    return true;
    } else if (_.isObject(value) && !_.isFunction(value)) {
    var foundKeyValueRec = keyLookup(value, string);
    // Call recursively
    if (foundKeyValueRec) {
    path = key + ' : ' + foundKeyValueRec;
    return true;
    }
    }
    });
    return path;
    }
    }

    // Test:

    var testObj = {
    someStuff: 1,
    someObjectStuff: {
    arr: [1, 2, 3]
    },
    a: {
    b: {
    func : function() {
    return 1;
    },
    c: [
    {
    test : {
    f : 1
    }
    },
    1
    ]
    }
    }
    }
    }

    keyLookup(testObj, "test") // returns "a : b : c : 0 : test"
  3. diegocard renamed this gist Jun 6, 2015. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  4. diegocard created this gist Jun 6, 2015.
    19 changes: 19 additions & 0 deletions gistfile1.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,19 @@
    keyLookup: function(obj, string) {
    if (_.isObject(obj)) {
    var path = null;
    _.find(obj, function(value, key) {
    if (_.isString(key) && key.includes(string)) {
    path = key.toString();
    return true;
    } else if (_.isObject(value) && !_.isFunction(value)) {
    var foundKeyValueRec = keyLookup(value, string);
    // Call recursively
    if (foundKeyValueRec) {
    path = key + ' : ' + foundKeyValueRec;
    return true;
    }
    }
    });
    return path;
    }
    }