Last active
August 29, 2015 14:05
-
-
Save NearlyUnique/8368ed239047540bf1ef to your computer and use it in GitHub Desktop.
find({abc:1,def:{ghi:"hi"}}, “ghi”) // finds the full object path to "def.ghi"
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*global console*/ | |
function find(target, searchFor, path) { | |
"use strict"; | |
try { | |
path = path || ""; | |
var i, attr, attrs = Object.keys(target); | |
searchFor = searchFor.toLowerCase(); | |
for (i = 0; i < attrs.length; i += 1) { | |
attr = attrs[i]; | |
if (attr.toLowerCase().indexOf(searchFor) >= 0) { | |
console.log((path + '.' + attr).substr(1)); | |
} | |
if (target[attr] && typeof target[attr] === "object") { | |
find(target[attr], searchFor, path + "." + attr); | |
} | |
} | |
} catch (e) { | |
console.log(target, searchFor, path, e); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment