Created
April 15, 2013 18:35
-
-
Save edfuh/5390267 to your computer and use it in GitHub Desktop.
dumb dirty way to find if an object key exists in a deep object
This file contains hidden or 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
keyExists = function(obj, structure) { | |
var F = function(){}; | |
F.prototype = obj; | |
var parts = structure.split('.'); | |
var testObj = new F; | |
for (var i = 0, j = parts.length; i < j; i++) { | |
if (!testObj[parts[i]]) { | |
return false; | |
} else { | |
testObj = testObj[parts[i]]; | |
} | |
} | |
return true; | |
} | |
var obj = { | |
a : { | |
b : { | |
c : { | |
d : { | |
e : { | |
f : { | |
g : { | |
h : { | |
i : 333 | |
} | |
} | |
} | |
} | |
} | |
} | |
} | |
} | |
}; | |
console.assert(keyExists(obj, 'a.b.c.d.e.f.g.h.i')); | |
console.assert( ! keyExists(obj, 'a.b.c.d.e.f.g.h.i.j.k') ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment