在AJAX请求之后,有时我的应用程序可能会返回一个空对象,例如:
var a = {};
我该如何检查是否是这种情况?
在AJAX请求之后,有时我的应用程序可能会返回一个空对象,例如:
var a = {};
我该如何检查是否是这种情况?
当前回答
我在用这个。
function isObjectEmpty(object) {
var isEmpty = true;
for (keys in object) {
isEmpty = false;
break; // exiting since we found that the object is not empty
}
return isEmpty;
}
Eg:
var myObject = {}; // Object is empty
var isEmpty = isObjectEmpty(myObject); // will return true;
// populating the object
myObject = {"name":"John Smith","Address":"Kochi, Kerala"};
// check if the object is empty
isEmpty = isObjectEmpty(myObject); // will return false;
从这里开始
使现代化
OR
可以使用isEmptyObject的jQuery实现
function isEmptyObject(obj) {
var name;
for (name in obj) {
return false;
}
return true;
}
其他回答
let jsObject = JSON.parse(JSON.stringify(obj), (key, value) => {
if (value === null ||
value === '' ||
(value.constructor === Object && Object.entries(value).length === 0) ||
(value.constructor === Array && value.length === 0)) {
return undefined
}
return value
})
这将递归地过滤掉所有无效字段。
这一行代码也有助于回退到较旧的浏览器。
var a = {}; //if empty returns false
(Object.getOwnPropertyNames ? Object.getOwnPropertyNames(a).length !== 0 : (function(){ for(var key in a) break; return !!key })()) //Returns False
var a = {b:2}; //if not empty returns true
(Object.getOwnPropertyNames ? Object.getOwnPropertyNames(a).length !== 0 : (function(){ for(var key in a) break; return !!key })()) //Returns true
Object.getOwnPropertyNames在ECMA-5中实现。上面的行在具有回退功能的旧浏览器中工作。
另一个快速解决方案是检查Object.keys、Object.entries或Object.values
知识文章:关注这篇SO文章,了解Object.keys与Object.getOwnPropertyNames之间的详细区别
完美的故障保护解决方案
我认为第一个被接受的解决方案在大多数情况下都有效,但不是故障保护。
更好的故障安全解决方案将是。
function isEmptyObject() {
return toString.call(obj) === "[object Object]"
&& Object.keys(obj).length === 0;
}
或ES6/7
const isEmptyObject = () => toString.call(obj) === "[object Object]"
&& Object.keys(obj).length === 0;
使用这种方法,如果obj设置为undefined或null,则代码不会中断。并返回null。
下面的示例显示了如何测试JavaScript对象是否为空,如果我们所说的空是指它没有自己的财产。
该脚本适用于ES6。
常量isEmpty=(obj)=>{如果(obj===空||obj==未定义||Array.isArray(obj)||对象类型!=='对象') {返回true;}return Object.getOwnPropertyNames(obj).length==0;};console.clear();console.log('---');console.log(isEmpty(“”));//真的console.log(isEmpty(33));//真的console.log(isEmpty([]));//真的console.log(isEmpty({}));//真的console.log(isEmpty({length:0,custom_property:[]}));//假的console.log('---');console.log(isEmpty('Hello'));//真的console.log(isEmpty([1,2,3]));//真的console.log(isEmpty({test:1}));//假的console.log(isEmpty({长度:3,自定义属性:[1,2,3]}));//假的console.log('---');console.log(isEmpty(new Date()));//真的console.log(isEmpty(无限));//真的console.log(isEmpty(null));//真的console.log(isEmpty(未定义));//真的
我能找到的最佳单线解决方案(已更新):
isEmpty=obj=>!Object.values(obj).filter(e=>类型e!=='undefined').length;console.log(isEmpty({}))//trueconsole.log(isEmpty({a:undefined,b:undefine}))//trueconsole.log(isEmpty({a:undefined,b:void 1024,c:void 0}))//trueconsole.log(isEmpty({a:[undefined,undefined]}))//falseconsole.log(isEmpty({a:1}))//falseconsole.log(isEmpty({a:“”}))//falseconsole.log(isEmpty({a:null,b:undefined}))//false