Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check whether an object is empty?

I'm learing JavaScript. I cannot grasp the idea of an empty object. As I understand, there are situations when I need to check a variable whether it holds an object and has a value.

So far, I know that a variable can be undefined.

var car; // the value is undefined, as well as the type (which is it? number, array, etc.)

I also know, that everything that has a value, is true:

var car = "Opel";
Boolean(car); // true 

And anything without a value is false:

var car = ""; // typeof string, but it is empty, so
Boolean(car); // false
Boolean(null); // false 

So why doesn't it work the same way with arrays and objects? Why is all of this true? Shouldn't empty arrays and objects return false?

var car = {make: "Opel", year: 2015};
Boolean(car); // true
car = {};
Boolean(car); // true
car = ["BMW", "Opel"]; 
Boolean(car); // true
car = [];
Boolean(car); // true

Now I see that there are methods, that can be applied to check an object's length, I just haven't reached that part yet.

I'm learning at W3Schools website and this bit just got me puzzled:

But you cannot test if an object is null, because this will throw an error if the object is undefined:

Incorrect:

if (myObj === null) 

To solve this problem, you must test if an object is not null, and not undefined.

But this can still throw an error:

Incorrect:

if (myObj !== null && typeof myObj !== "undefined")

Because of this, you must test for not undefined before you can test for not null:

Correct:

if (typeof myObj !== "undefined" && myObj !== null)

I still cannot understand the last line here.

like image 572
VantaBlack Avatar asked Jan 27 '21 19:01

VantaBlack


People also ask

How do you check if an object is an empty object in Java?

Properties isEmpty() method in Java with Examples The isEmpty() method of Properties class is used to check if this Properties object is empty or not. Returns: This method returns a boolean value stating if this Properties object is empty or not.

How check if object is empty array?

To check if an array is empty or not, you can use the . length property. The length property sets or returns the number of elements in an array. By knowing the number of elements in the array, you can tell if it is empty or not.

How do you know if an object has no property?

We can check if a property exists in the object by checking if property !== undefined . In this example, it would return true because the name property does exist in the developer object.

How to check if an object is empty using JavaScript?

How to check an object is empty using JavaScript? Method 1: Using the Object.keys (object) method: The required object could be passed to the Object.keys (object) method which will return the keys in the object. The length property is used to the result to check the number of keys.

How to check the number of keys in an object?

Method 1: Using the Object.keys (object) method: The required object could be passed to the Object.keys (object) method which will return the keys in the object. The length property is used to the result to check the number of keys. If the length property returns 0 keys, it means that the object is empty.

How to check if an object is empty in JSON?

This is the simplest way to check if an object is empty. If we stringify the object and the result is simply an opening and closing bracket, we know the object is empty. function isEmptyObject (obj) { return JSON.stringify (obj) === ' {}'; } The for…in statement will loop through the enumerable property of the object.

How do I check if a string is empty?

For "empty" check, it depends on what type is involved. For a Java String, you usually do: if(str != null && !str.isEmpty()) As you haven't mentioned about any specific problem with this, difficult to tell. Share Improve this answer Follow answered Jan 22 '13 at 16:29 SwapnilSwapnil


3 Answers

Checking if an object is empty:

Object.keys(yourObject).length
like image 67
AdriSolid Avatar answered Oct 29 '22 00:10

AdriSolid


var car = {};
var isEmpty = Object.entries(car).length > 0; //false

var car = {make: "Opel", year: 2015};
var isEmpty = Object.entries(car).length > 0; //true

This should solve your problem, if your curious about the utility function Object.entries you can look on mdn

like image 39
Other Me Avatar answered Oct 29 '22 01:10

Other Me


You can check wheather a object is empty or not in this simple way.

function objectLength( object ) {
    return Object.keys(object).length;
}

if(objectLength(yourObject) == 0){ 
    //object is empty
}else{
    //object not empty 
}
like image 28
infomasud Avatar answered Oct 29 '22 00:10

infomasud