62

I have a function that checks to see whether or not a request has any queries, and does different actions based off that. Currently, I have if(query) do this else something else. However, it seems that when there is no query data, I end up with a {} JSON object. As such, I need to replace if(query) with if(query.isEmpty()) or something of that sort. Can anybody explain how I could go about doing this in NodeJS? Does the V8 JSON object have any functionality of this sort?

2
  • wouldn't if(0 < query.length) { ... } work? Commented Jul 14, 2012 at 3:39
  • 1
    naaa, query.length == undefined. length is standard in an Array Object, not a JSON Object. Commented Jul 14, 2012 at 4:10

6 Answers 6

122

You can use either of these functions:

// This should work in node.js and other ES5 compliant implementations. function isEmptyObject(obj) { return !Object.keys(obj).length; } // This should work both there and elsewhere. function isEmptyObject(obj) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { return false; } } return true; } 

Example usage:

if (isEmptyObject(query)) { // There are no queries. } else { // There is at least one query, // or at least the query object is not empty. } 
Sign up to request clarification or add additional context in comments.

Comments

38

You can use this:

var isEmpty = function(obj) { return Object.keys(obj).length === 0; } 

or this:

function isEmpty(obj) { return !Object.keys(obj).length > 0; } 

You can also use this:

function isEmpty(obj) { for(var prop in obj) { if(obj.hasOwnProperty(prop)) return false; } return true; } 

If using underscore or jQuery, you can use their isEmpty or isEmptyObject calls.

Comments

22
Object.keys(myObj).length === 0; 

As there is need to just check if Object is empty it will be better to directly call a native method Object.keys(myObj).length which returns the array of keys by internally iterating with for..in loop.As Object.hasOwnProperty returns a boolean result based on the property present in an object which itself iterates with for..in loop and will have time complexity O(N2).

On the other hand calling a UDF which itself has above two implementations or other will work fine for small object but will block the code which will have severe impact on overall perormance if Object size is large unless nothing else is waiting in the event loop.

Comments

3
const isEmpty = (value) => ( value === undefined || value === null || (typeof value === 'object' && Object.keys(value).length === 0) || (typeof value === 'string' && value.trim().length === 0) ) module.exports = isEmpty; 

Comments

2

If you have compatibility with Object.keys, and node does have compatibility, you should use that for sure.

However, if you do not have compatibility, and for any reason using a loop function is out of the question - like me, I used the following solution:

JSON.stringify(obj) === '{}' 

Consider this solution a 'last resort' use only if must.

See in the comments "there are many ways in which this solution is not ideal".

I had a last resort scenario, and it worked perfectly.

2 Comments

There are any number of reasons this isn't a great solution, including performance, and the variety of ways objects can be stringified, e.g., stringify an Error with properties.
@DaveNewton, , I agree that this isn't a great solution. I state that in my answer. I do not agree about the Error example since Object.keys(obj).length === 0 would return empty as well, so in this scenario this solution is still correct.
2

My solution:

let isEmpty = (val) => { let typeOfVal = typeof val; switch(typeOfVal){ case 'object': return (val.length == 0) || !Object.keys(val).length; break; case 'string': let str = val.trim(); return str == '' || str == undefined; break; case 'number': return val == ''; break; default: return val == '' || val == undefined; } }; console.log(isEmpty([1,2,4,5])); // false console.log(isEmpty({id: 1, name: "Trung",age: 29})); // false console.log(isEmpty('TrunvNV')); // false console.log(isEmpty(8)); // false console.log(isEmpty('')); // true console.log(isEmpty(' ')); // true console.log(isEmpty([])); // true console.log(isEmpty({})); // true 

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.