27

I am trying to create to javascript/jquery test to check if my object is empty and cannot figure it out.

Here is the object when it has something in it:

{"mergedSellerArray":{"key1114":"1120"}}

And here is the object when empty:

{"mergedSellerArray":{}}

This is the current test I have based on another SO answer but it does not work:

var sellers = JSON.stringify({mergedSellerArray});
if(Object.keys(sellers).length === 0 && sellers.constructor === Object) {
    console.log("sellers is empty!");
}
8
  • 2
    what is sellers? Commented Mar 15, 2017 at 15:17
  • 1
    If sellers is the outer object, then it isn't empty, and you'll have to write some code to check that it matches your expected state. Commented Mar 15, 2017 at 15:18
  • What's the point of testing the constructor? Commented Mar 15, 2017 at 15:18
  • added in the sellers declaration Commented Mar 15, 2017 at 15:19
  • 2
    JSON.stringify() turns the object into a string, as the name suggests. What's the point of the sellers variable anyway? Why can't you just check mergedSellerArray as is? Commented Mar 15, 2017 at 15:20

5 Answers 5

48

You were testing sellers which is not empty because it contains mergedSellerArray. You need to test sellers.mergedSellerArray

let sellers = {
  "mergedSellerArray": {}
};
if (Object.keys(sellers.mergedSellerArray).length === 0 && sellers.mergedSellerArray.constructor === Object) {
  console.log("sellers is empty!");
} else {
  console.log("sellers is not empty !");
}

Sign up to request clarification or add additional context in comments.

1 Comment

Downvoted for performance. The entire object is mapped to an array with the use of Object.keys, which takes O(n) or worse (depending on hash implementation), which imo is pretty irresponsible for a simple "isEmpty" check. An O(1) implementation: let hasNoKeys = obj => { for (let k in obj) return false; return true; }
5

If you are using lodash library, you have an elegant way to check an empty object, array, map or a set. I presume you are aware of ES6 Import statement.

import {isEmpty} from "lodash"

let obj = {};
console.log(isEmpty(obj)); //Outputs true.

let arr = [];
console.log(isEmpty(arr)); //Outputs true.

obj.name="javascript";
console.log(isEmpty(obj)); //Outputs false.

So, for your code,

isEmpty(mergedSellerArray); //will return true if object is not empty.

Hope this answer helped.

Comments

4

This will work in modern web browser. It is quite easy and simple

const empty = {};
if(Object.keys(empty).length === 0 && empty.constructor === Object) {
    console.log("Object is empty");
} else {
    console.log("Object is not empty");
}

Comments

4

Here is in jQuery:

$(document).ready(function(){
  var obj={"mergedSellerArray":{}};
  alert("is empty: "+$.isEmptyObject(obj.mergedSellerArray));

  var obj2={"mergedSellerArray":{"key1114":"1120"}};
  alert("is empty: "+$.isEmptyObject(obj2.mergedSellerArray));
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js" />

jsfidle: https://jsfiddle.net/nyqgbp38/

2 Comments

The entire jQuery library to test if an object is empty?!
It is mentioned that the solution in jquery. thanks
2

Can create the helper function :

const isEmpty = inputObject => {
  return Object.keys(inputObject).length === 0;
};

Can use it like:

let inputObject = {};
console.log(isEmpty(inputObject))  // true.

and

inputObject = {name: "xyz"}; 
console.log(isEmpty(inputObject)) // false

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.