首页

JavaScript判断变量是否为空对象 {} 的几种方法

seo达人

如果您想订阅本博客内容,每天自动发到您的邮箱中, 请点这里

json对象转化为json字符串,再判断该字符串是否为"{}"

var obj = {}; var b = (JSON.stringify(obj) === "{}");
console.log(b); // true
    
  • 1
  • 2
  • 3

for in 循环判断

var obj = {}; var b = function() { for(var key in obj) { return false;
    } return true;
}
console.log(b()); // true
    
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

网上很多都是如上的说法,但是会存在一个问题,就是如果obj=null ,obj=undefinedobj=""obj=[]obj=0 以及obj为任意数字也返回true,所以有了下面这个for in 循环判断:

方案一:

var obj = {}; var b = function() { for(var key in obj) { return false;
    } if(obj === null || typeof obj !== "object" || Array.isArray(obj)){ return false;
    } return true;
}
console.log(b()); // true
    
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

方案二:

var obj = {}; var b = function() { for(var key in obj) { return false;
    } if(obj === null || typeof obj !== "object" || Object.prototype.toString.call(obj) === "[object Array]"){ return false;
    } return true;
}
console.log(b()); // true
    
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

上面两种方案的区别就是判断判断空数组的方式不同。

jQuery的jQuery.isEmptyObject(obj)方法

var obj = {}; var b = $.isEmptyObject(obj);
console.log(b); // true
    
  • 1
  • 2
  • 3

jQuery.isEmptyObject(obj) 方法依然存在obj=null ,obj=undefinedobj=""obj=[]obj=0 以及obj为任意数字返回true的问题,所以我们还应该再用typeof 或者 $.type() 判断一下:

var obj = {}; var b = $.isEmptyObject(obj) && $.type(obj) === "object";
console.log(b); // true
    
  • 1
  • 2
  • 3
var obj = {}; var b = $.isEmptyObject(obj) && typeof obj === "object" && obj !== null && !Array.isArray(obj);
console.log(b); // true
    
  • 1
  • 2
  • 3
var obj = {}; var b = $.isEmptyObject(obj) && typeof obj === "object" && obj !== null && Object.prototype.toString.call(obj) !== "[object Array]";
console.log(b); // true
    
  • 1
  • 2
  • 3

Object.getOwnPropertyNames()方法

Object.getOwnPropertyNames() 方法返回一个由指定对象的所有自身属性的属性名(包括不可枚举属性但不包括Symbol值作为名称的属性)组成的数组。

var obj = {}; var b = !Object.getOwnPropertyNames(obj).length;
console.log(b); // true
    
  • 1
  • 2
  • 3

Object.getOwnPropertyNames() 方法存在obj=0 以及obj为任意数字返回true的问题,所以我们还应该再用typeof 判断一下:

var obj = {}; var b = !Object.getOwnPropertyNames(obj).length && typeof obj === "object";
console.log(b); // true
    
  • 1
  • 2
  • 3

Object.keys()方法

Object.keys() 方法会返回一个由一个给定对象的自身可枚举属性组成的数组,数组中属性名的排列顺序和使用 for...in循环遍历该对象时返回的顺序一致 。

var obj = {}; var b = !Object.keys(obj).length;
console.log(b); // true
    
  • 1
  • 2
  • 3

Object.keys() 方法存在obj=""obj=[]obj=0 以及obj为任意数字返回true的问题,所以依旧需要加判断如下:

var obj = {}; var b = !Object.keys(obj).length && typeof obj === "object" && !Array.isArray(obj);
console.log(b); // true
    
  • 1
  • 2
  • 3
var obj = {}; var b = !Object.keys(obj).length && typeof obj === "object" && Object.prototype.toString.call(obj) !== "[object Array]";
console.log(b); // true
    
  • 1
  • 2
  • 3

在实际应用中,如果对象不为空,并且知道对象不为空时,某个属性一定存在,则直接判断这个对象的此属性是否存在。

蓝蓝设计www.lanlanwork.com )是一家专注而深入的界面设计公司,为期望卓越的国内外企业提供卓越的UI界面设计、BS界面设计 、 cs界面设计 、 ipad界面设计 、 包装设计 、 图标定制 、 用户体验 、交互设计、 网站建设 平面设计服务

日历

链接

blogger

蓝蓝 http://www.lanlanwork.com

存档