How to check if object has any properties in JavaScript?

后端 未结 16 2355
自闭症患者
自闭症患者 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  
    }
    

提交回复
热议问题