Skip to content

Instantly share code, notes, and snippets.

@edfuh
Created April 15, 2013 18:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save edfuh/5390267 to your computer and use it in GitHub Desktop.
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
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