Home > Web Front-end > JS Tutorial > Can I use js to determine the json empty object?

Can I use js to determine the json empty object?

一个新手
Release: 2017-09-08 13:27:31
Original
2288 people have browsed it

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
Copy after login
Copy after login
Copy after login


print?
var dd={yy:""};  
   if(dd&&dd.yy!=""){  
       alert("dd不为空")  
   }  
   else{  
       alert("dd为空")  
   }
Copy after login


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
Copy after login
Copy after login
Copy after login


print?
var a={};  
ar b=new Object();  
console.log("对象字面量的比较结果:"+(JSON.stringify(a)=="{}"))  
onsole.log("构造函数的比较结果:"+(JSON.stringify(b)=="{}"))
Copy after login


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
Copy after login
Copy after login
Copy after login


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是个空对象")  
}
Copy after login

4. Our commonly used jquery also provides a method:

$.isEmptyObjec({})
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template