/ Published in: JavaScript
http://blog.ramonlechuga.com/2010/10/20/checking-object-structure/
The Array
The Array
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
/* * Some frameworks & browsers include this array method */ 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.item')); // true console.log(isSet(object,'data.item.sub_item.bla.here.iam')); // true
URL: http://blog.ramonlechuga.com/2010/10/20/checking-object-structure/