Posted By

ramonlechuga on 03/08/11


Tagged

object check properties


Versions (?)

Checking Object Properties


 / Published in: JavaScript
 

URL: http://blog.ramonlechuga.com/2010/10/20/checking-object-structure/

http://blog.ramonlechuga.com/2010/10/20/checking-object-structure/

The Array

  1. /*
  2. * Some frameworks & browsers include this array method
  3. */
  4. Array.prototype.some = function(fn, thisObj) {
  5. var scope = thisObj || window;
  6. for ( var i=0, j=this.length; i < j; ++i ) {
  7. if ( fn.call(scope, this[i], i, this) ) {
  8. return true;
  9. }
  10. }
  11. return false;
  12. };
  13.  
  14.  
  15. function isSet (object, string) {
  16. if (!object) return false;
  17. var childs = string.split('.');
  18. if (childs.length > 0 ) {
  19. return !childs.some(function (item) {
  20. if (item in object) {
  21. object = object[item];
  22. return false;
  23. } else return true;
  24. });
  25. } else if (string in object) {
  26. return true;
  27. } else return false;
  28. }
  29.  
  30. var object = {
  31. data: {
  32. item: {
  33. sub_item: {
  34. bla: {
  35. here : {
  36. iam: true
  37. }
  38. }
  39. }
  40. }
  41. }
  42. };
  43.  
  44. console.log(isSet(object,'data.item')); // true
  45. console.log(isSet(object,'x')); // false
  46. console.log(isSet(object,'data.item')); // true
  47. console.log(isSet(object,'data.item.sub_item.bla.here.iam')); // true

Report this snippet  

You need to login to post a comment.