-
js判断对象是否为空
2020-11-05 18:25:53js判断对象是否为空 //判断变量是否为空 function isNull(obj){ const a = getVariableType(obj); console.log(a) switch (a) { case "undefined": return true; case "string": obj = obj....js判断对象是否为空
//判断变量是否为空 function isNull(obj){ const a = getVariableType(obj); console.log(a) switch (a) { case "undefined": return true; case "string": obj = obj.trim(); if (obj=='' || obj==null || obj =="undefined"){ return true; }else{ return false; } case "boolean": return false; case "number": return false; case "array": if(obj.length==0){ return true; }else{ return false; } case "json": var str = JSON.stringify(obj); if(str=="{}"){ return true; }else{ return false; } case "function": return false; case "date": return false; default: return true; } } function isNotNull(i){ return !isNull(i); } //获取变量的类型 function getVariableType(obj){ if(obj==null){ return "undefined"; }else{ var type = typeof(obj); if(type=="object"){ if(obj.constructor==Array){//数组 return "array" }else if(isDate(obj)){ return "date"; }else if(isJSON (obj)){ return "json" }else{ return "undefined"; } }else{ return type; } } } //判断对象是否是时间 function isDate(obj){ return obj instanceof Date; } //判断对象是否是json function isJSON (obj) { if (typeof obj == 'string') { try { var obj2 = JSON.parse(obj); if (typeof obj2 == 'object' && obj2) { return true; } else { return false; } } catch (e) { console.log('error:' + str + '!!!' + e); return false; } } else if(typeof obj == 'object'){ if(obj.constructor==Array){ return false; }else{ try { var str = JSON.stringify(obj); return true; }catch (e) { console.log('error:' + str + '!!!' + e); return false; } } } }
-
JS 判断对象是否为空
2018-09-12 08:49:03版权声明:本文首发 http://asing1elife.com ,转载请注明出处。 https://blog.csdn.net/asing1elife/article/details/82655689 JS 判断对象是否为空 ES...版权声明:本文首发 http://asing1elife.com ,转载请注明出处。 https://blog.csdn.net/asing1elife/article/details/82655689JS 判断对象是否为空
ES6 新增了 Object.keys() 可将对象自身可枚举属性转换为数组
更多精彩
- 更多技术博客,请移步 asing1elife’s blog
实现方式
let obj = {} let objArr = Object.keys(obj) console.log(objArr.length) // 0
-
js 判断对象是否为空
2020-09-22 16:27:49可以通过判断返回的数组长度来判断对象是否为空。 // 测试数据 let obj= {}; let tempArr = Object.keys(obj); if (tempArr.length) { console.log('对象不为空') } else { console.log('对象为空') } ...Object.keys(obj)
Object.keys
返回一个数组,包括对象自身的(不含继承的)所有可枚举属性(不含 Symbol 属性)的键名。可以通过判断返回的数组长度来判断对象是否为空。
// 测试数据 let obj= {}; let tempArr = Object.keys(obj); if (tempArr.length) { console.log('对象不为空') } else { console.log('对象为空') }
-
JS判断对象是否为空
2021-03-26 19:09:47console.log("对象为空"); }else { console.log("对象不为空"); } 2.JSON.stringify() var obj = {}; if(JSON.stringify(obj)=== '{}') { console.log("对象为空"); }else { console.log("对象不为空1.Object.keys(obj).length
var obj = {}; if (Object.keys(obj).length == 0) { console.log("对象为空"); }else { console.log("对象不为空"); }
2.JSON.stringify()
var obj = {}; if(JSON.stringify(obj)=== '{}') { console.log("对象为空"); }else { console.log("对象不为空"); }
3.for in 遍历属性
function isEmptyObj(data) { for(var item in data) { return false; } return true; }; var obj = {}; if (isEmptyObj(obj)){ console.log("对象为空"); }else { console.log("对象不为空"); }
-
Js判断对象是否为空
2019-02-15 23:05:311.for (... in ...) for(var i in obj){ return true; //如果不为空,返回true } return false; //如果为空,返回false ...2.JSON.stringify() ...if(JSON.stringify(data) === '{}'){ ... //如果为空,返回fals...
收藏数
797
精华内容
318