We want to determine whether the object is empty. It is not possible to determine whether the object is empty like the basic type, ==={}? This is wrong because it only compares whether the reference addresses are the same, so the following method can be used to judge.
1. Traverse the object according to for...in, return true if it exists, otherwise return false
for ( let i in obj) { return true; } return false
Online video learning sharing: java online video
2. Use the JSON.stringify() method that comes with JSON to judge
The general idea is to convert it into a string '{}' for judgment.
if (JSON.stringify(obj) === '{}') { return true; } return false;
3. Use Object.keys() in ES6 to make judgments (recommended)
The Object.keys() method will return an enumerable property consisting of a given object. array. If our object is empty, it will return an empty array.
Object.keys(obj).length === 0 ? '空' : '不为空'
More related articles and tutorials are recommended: javaQuick Start
The above is the detailed content of Summary of methods to determine whether an object is empty in Java. For more information, please follow other related articles on the PHP Chinese website!