Home  >  Article  >  Web Front-end  >  How to determine whether an object is empty in es6

How to determine whether an object is empty in es6

青灯夜游
青灯夜游Original
2022-10-20 16:39:004710browse

Judgment method: 1. Use "Object.keys()" to judge, the syntax is "Object.keys(obj).length === 0&&obj.constructor===Object ", return true to indicate empty; 2 . Convert the object into a json string and determine whether the string is "{}"; 3. Use isEmptyObject() to determine, the syntax is "$.isEmptyObject(data)", return true to indicate it is empty.

How to determine whether an object is empty in es6

The operating environment of this tutorial: Windows 7 system, ECMAScript version 6, Dell G3 computer.

To determine whether the object is empty is to determine whether the object is an empty object.

ES6 various methods to determine whether it is an empty object

1. Use the ES6 Object.keys() method

Object.keys() is a new method in ES6. The return value is also an array composed of property names in the object, including all enumerable properties of the object itself (excluding inherited ones) (excluding Symbol attribute) key name.

Object.keys(obj).length === 0 && obj.constructor === Object  
// true表示为空对象,false为非空对象

Disadvantages: Some browsers do not support it and need to be converted to ES5 through the babel class plug-in. Usage examples are as follows:

var data = {};

var arr = Object.keys(data);

alert(arr.length == 0);//true 即为空对象

var datas={a:1,b:2};

var aRR = Object.keys(datas);

console.log(aRR) -->   ['a','b']

2. Convert the object into a json string, and then determine whether the string is "{}"

var data = {};
var b = (JSON.stringify(data) == "{}");
alert(b);//true

3. for in loop judgment

var obj = {};
var b = function() {
	for(var key in obj) {
		return false;
	}
	return true;
}
alert(b());//true

4. jquery’s isEmptyObject method

This method encapsulates 3 methods (for in) by jquery. When using it, you need to rely on jquery

var data = {};
var b = $.isEmptyObject(data);
alert(b);//true

5 and Object.getOwnPropertyNames() method

This method uses the getOwnPropertyNames method of the Object object to obtain the property names in the object, store them in an array, and return the array object. We can determine whether the object is empty by judging the length of the array.

Note: This method is not compatible with ie8, and other browsers have not been tested.

var data = {};
var arr = Object.getOwnPropertyNames(data);
alert(arr.length == 0);//true

[Related recommendations: javascript video tutorial, programming video

The above is the detailed content of How to determine whether an object is empty in es6. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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