How to check if object has any properties in JavaScript?

后端 未结 16 2204
自闭症患者
自闭症患者 2020-12-04 08:10

Assuming I declare

var ad = {}; 

How can I check whether this object will contain any user-defined properties?

相关标签:
16条回答
  • 2020-12-04 08:55

    You can use the built in Object.keys method to get a list of keys on an object and test its length.

    var x = {};
    // some code where value of x changes and than you want to check whether it is null or some object with values
    
    if(Object.keys(x).length > 0){
     // Your code here if x has some properties  
    }
    
    0 讨论(0)
  • 2020-12-04 08:55

    With jQuery you can use:

    $.isEmptyObject(obj); // Returns: Boolean
    

    As of jQuery 1.4 this method checks both properties on the object itself and properties inherited from prototypes (in that it doesn't use hasOwnProperty).

    With ECMAScript 5th Edition in modern browsers (IE9+, FF4+, Chrome5+, Opera12+, Safari5+) you can use the built in Object.keys method:

    var obj = { blah: 1 };
    var isEmpty = !Object.keys(obj).length;
    

    Or plain old JavaScript:

    var isEmpty = function(obj) {
                   for(var p in obj){
                      return false;
                   }
                   return true;
                };
    
    0 讨论(0)
  • 2020-12-04 08:56

    If you are willing to use lodash, you can use the some method.

    _.some(obj) // returns true or false
    

    See this small jsbin example

    0 讨论(0)
  • 2020-12-04 08:57

    What about making a simple function?

    function isEmptyObject(obj) {
      for(var prop in obj) {
        if (Object.prototype.hasOwnProperty.call(obj, prop)) {
          return false;
        }
      }
      return true;
    }
    
    isEmptyObject({}); // true
    isEmptyObject({foo:'bar'});  // false
    

    The hasOwnProperty method call directly on the Object.prototype is only to add little more safety, imagine the following using a normal obj.hasOwnProperty(...) call:

    isEmptyObject({hasOwnProperty:'boom'});  // false
    

    Note: (for the future) The above method relies on the for...in statement, and this statement iterates only over enumerable properties, in the currently most widely implemented ECMAScript Standard (3rd edition) the programmer doesn't have any way to create non-enumerable properties.

    However this has changed now with ECMAScript 5th Edition, and we are able to create non-enumerable, non-writable or non-deletable properties, so the above method can fail, e.g.:

    var obj = {};
    Object.defineProperty(obj, 'test', { value: 'testVal', 
      enumerable: false,
      writable: true,
      configurable: true
    });
    isEmptyObject(obj); // true, wrong!!
    obj.hasOwnProperty('test'); // true, the property exist!!
    

    An ECMAScript 5 solution to this problem would be:

    function isEmptyObject(obj) {
      return Object.getOwnPropertyNames(obj).length === 0;
    }
    

    The Object.getOwnPropertyNames method returns an Array containing the names of all the own properties of an object, enumerable or not, this method is being implemented now by browser vendors, it's already on the Chrome 5 Beta and the latest WebKit Nightly Builds.

    Object.defineProperty is also available on those browsers and latest Firefox 3.7 Alpha releases.

    0 讨论(0)
  • 2020-12-04 08:58
    var hasAnyProps = false; for (var key in obj) { hasAnyProps = true; break; }
    // as of this line hasAnyProps will show Boolean whether or not any iterable props exist
    

    Simple, works in every browser, and even though it's technically a loop for all keys on the object it does NOT loop through them all...either there's 0 and the loop doesn't run or there is some and it breaks after the first one (because all we're checking is if there's ANY...so why continue?)

    0 讨论(0)
  • 2020-12-04 09:02
    for(var memberName in ad)
    {
      //Member Name: memberName
      //Member Value: ad[memberName]
    }
    

    Member means Member property, member variable, whatever you want to call it >_>

    The above code will return EVERYTHING, including toString... If you only want to see if the object's prototype has been extended:

    var dummyObj = {};  
    for(var memberName in ad)
    {
      if(typeof(dummyObj[memberName]) == typeof(ad[memberName])) continue; //note A
      //Member Name: memberName
      //Member Value: ad[memberName]
    
    }
    

    Note A: We check to see if the dummy object's member has the same type as our testing object's member. If it is an extend, dummyobject's member type should be "undefined"

    0 讨论(0)
提交回复
热议问题