How to check if object has any properties in JavaScript?

后端 未结 16 2202
自闭症患者
自闭症患者 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:44

    How about this?

    var obj = {},
    var isEmpty = !obj;
    var hasContent = !!obj
    
    0 讨论(0)
  • 2020-12-04 08:45

    Very late answer, but this is how you could handle it with prototypes.

    Array.prototype.Any = function(func) {
        return this.some(func || function(x) { return x });
    }
    
    Object.prototype.IsAny = function() {
        return Object.keys(this).Any();
    }
    
    0 讨论(0)
  • 2020-12-04 08:45

    You can use the following:

    Double bang !! property lookup

    var a = !![]; // true
    var a = !!null; // false
    

    hasOwnProperty This is something that I used to use:

    var myObject = {
      name: 'John',
      address: null
    };
    if (myObject.hasOwnProperty('address')) { // true
      // do something if it exists.
    }
    

    However, JavaScript decided not to protect the method’s name, so it could be tampered with.

    var myObject = {
      hasOwnProperty: 'I will populate it myself!'
    };
    

    prop in myObject

    var myObject = {
      name: 'John',
      address: null,
      developer: false
    };
    'developer' in myObject; // true, remember it's looking for exists, not value.
    

    typeof

    if (typeof myObject.name !== 'undefined') {
      // do something
    }
    

    However, it doesn't check for null.

    I think this is the best way.

    in operator

    var myObject = {
      name: 'John',
      address: null
    };
    
    if('name' in myObject) {
      console.log("Name exists in myObject");
    }else{
      console.log("Name does not exist in myObject");
    }
    

    result:

    Name exists in myObject

    Here is a link that goes into more detail on the in operator: Determining if an object property exists

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

    When sure that the object is a user-defined one, the easiest way to determine if UDO is empty, would be the following code:

    isEmpty=
    /*b.b Troy III p.a.e*/
    function(x,p){for(p in x)return!1;return!0};
    

    Even though this method is (by nature) a deductive one, - it's the quickest, and fastest possible.

    a={};
    isEmpty(a) >> true
    
    a.b=1
    isEmpty(a) >> false 
    

    p.s.: !don't use it on browser-defined objects.

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

    You can loop over the properties of your object as follows:

    for(var prop in ad) {
        if (ad.hasOwnProperty(prop)) {
            // handle prop as required
        }
    }
    

    It is important to use the hasOwnProperty() method, to determine whether the object has the specified property as a direct property, and not inherited from the object's prototype chain.

    Edit

    From the comments: You can put that code in a function, and make it return false as soon as it reaches the part where there is the comment

    Performance Test

    Test Of Object.Keys vs For..In When testing for any properties

    0 讨论(0)
  • 2020-12-04 08:50
    for (var hasProperties in ad) break;
    if (hasProperties)
        ... // ad has properties
    

    If you have to be safe and check for Object prototypes (these are added by certain libraries and not there by default):

    var hasProperties = false;
    for (var x in ad) {
        if (ad.hasOwnProperty(x)) {
            hasProperties = true;
            break;
        }
    }
    if (hasProperties)
        ... // ad has properties
    
    0 讨论(0)
提交回复
热议问题