Created
March 23, 2011 00:41
-
-
Save alejandrolechuga/882412 to your computer and use it in GitHub Desktop.
Object Property Checker
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
//Just in case is not supported or not included by your framework | |
//*************************************************** | |
Array.prototype.some = function(fn, thisObj) { | |
var scope = thisObj || window; | |
for ( var i=0, j=this.length; i < j; ++i ) { | |
if ( fn.call(scope, this[i], i, this) ) { | |
return true; | |
} | |
} | |
return false; | |
}; | |
//**************************************************** | |
function isSet (object, string) { | |
if (!object) return false; | |
var childs = string.split('.'); | |
if (childs.length > 0 ) { | |
return !childs.some(function (item) { | |
if (item in object) { | |
object = object[item]; | |
return false; | |
} else return true; | |
}); | |
} else if (string in object) { | |
return true; | |
} else return false; | |
} | |
var object = { | |
data: { | |
item: { | |
sub_item: { | |
bla: { | |
here : { | |
iam: true | |
} | |
} | |
} | |
} | |
} | |
}; | |
console.log(isSet(object,'data.item')); // true | |
console.log(isSet(object,'x')); // false | |
console.log(isSet(object,'data.sub_item')); // false | |
console.log(isSet(object,'data.item')); // true | |
console.log(isSet(object,'data.item.sub_item.bla.here.iam')); // true |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment