Home > Article > Web Front-end > Can I use js to determine the json empty object?
1. The simplest method is to determine whether there is a corresponding attribute. Of course, it cannot be determined whether it is empty.
[javascript] view plain copy
print? var dd={yy:""}; if(dd&&dd.yy!=""){ alert("dd不为空") } else{ alert("dd为空") }
2. Determine by parsing json into a string function [ You can also use this to determine whether the backend is passing an array or an object]
[javascript] view plain copy
print? var a={}; ar b=new Object(); console.log("对象字面量的比较结果:"+(JSON.stringify(a)=="{}")) onsole.log("构造函数的比较结果:"+(JSON.stringify(b)=="{}"))
3. Judge by traversing, if there is an attribute, just return false directly and break at the same time Interrupt traversal
[javascript] view plain copy
print? var a={}; var b=new Object(); function isEmptyObject(obj){ for(var key in obj){ break; return false }; return true }; if(isEmptyObject(a)){ alert("对象为空") } if(isEmptyObject(b)){ alert("b是个空对象") }
4. Our commonly used jquery also provides a method:
$.isEmptyObjec({})
The above is the detailed content of Can I use js to determine the json empty object?. For more information, please follow other related articles on the PHP Chinese website!