How to match an empty dictionary in Javascript?

JavascriptDictionaryMatch

Javascript Problem Overview


From the node REPL thing,

> d = {}
{}
> d === {}
false
> d == {}
false

Given I have an empty dictionary, how do I make sure it is an empty dictionary ?

Javascript Solutions


Solution 1 - Javascript

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

Solution 2 - Javascript

You could extend Object.prototype with this isEmpty method to check whether an object has no own properties:

Object.prototype.isEmpty = function() {
    for (var prop in this) if (this.hasOwnProperty(prop)) return false;
    return true;
};

Solution 3 - Javascript

How about using jQuery?

$.isEmptyObject(d)

Solution 4 - Javascript

Since it has no attributes, a for loop won't have anything to iterate over. To give credit where it's due, I found this suggestion here.

function isEmpty(ob){
   for(var i in ob){ return false;}
  return true;
}

isEmpty({a:1}) // false
isEmpty({}) // true

Solution 5 - Javascript

This is what jQuery uses, works just fine. Though this does require the jQuery script to use isEmptyObject.

isEmptyObject: function( obj ) {
	for ( var name in obj ) {
		return false;
	}
	return true;
}

//Example
var temp = {};
$.isEmptyObject(temp); // returns True
temp ['a'] = 'some data';
$.isEmptyObject(temp); // returns False

If including jQuery is not an option, simply create a separate pure javascript function.

function isEmptyObject( obj ) {
    for ( var name in obj ) {
    	return false;
    }
    return true;
}

//Example
var temp = {};
isEmptyObject(temp); // returns True
temp ['b'] = 'some data';
isEmptyObject(temp); // returns False

Solution 6 - Javascript

I'm far from a JavaScript scholar, but does the following work?

if (Object.getOwnPropertyNames(d).length == 0) {
   // object is empty
}

It has the advantage of being a one line pure function call.

Solution 7 - Javascript

If performance isn't a consideration, this is a simple method that's easy to remember:

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

Obviously you don't want to be stringifying large objects in a loop, though.

Solution 8 - Javascript

You'd have to check that it was of type 'object' like so:

(typeof(d) === 'object')

And then implement a short 'size' function to check it's empty, as mentioned here.

Solution 9 - Javascript

var SomeDictionary = {};
if(jQuery.isEmptyObject(SomeDictionary))
// Write some code for dictionary is empty condition
else
// Write some code for dictionary not empty condition

This Works fine.

Solution 10 - Javascript

If you try this on Node.js use this snippet, based on this code here

Object.defineProperty(Object.prototype, "isEmpty", {
    enumerable: false,
    value: function() {
    	    for (var prop in this) if (this.hasOwnProperty(prop)) return false;
    	    return true;
    	}
	}
);

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJoão Pinto JerónimoView Question on Stackoverflow
Solution 1 - JavascriptRaynosView Answer on Stackoverflow
Solution 2 - JavascriptGumboView Answer on Stackoverflow
Solution 3 - JavascriptstrozView Answer on Stackoverflow
Solution 4 - JavascriptDavid RuttkaView Answer on Stackoverflow
Solution 5 - JavascriptcevarisView Answer on Stackoverflow
Solution 6 - JavascriptSteve BennettView Answer on Stackoverflow
Solution 7 - JavascriptSteve BennettView Answer on Stackoverflow
Solution 8 - Javascriptchrisfrancis27View Answer on Stackoverflow
Solution 9 - JavascriptVAMSHI PAIDIMARRIView Answer on Stackoverflow
Solution 10 - JavascriptarbyterView Answer on Stackoverflow