_.isEmpty(obj, true)
var obj = {
'firstName': undefined
, 'lastName' : undefined
};
console.log(_.isEmpty(obj)); // false
console.log(_.isEmpty({})); // true
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.js"></script>
Run code snippetEdit code snippet Hide Results Copy to answer Expand
Please, see http://www.ericfeminella.com/blog/2012/08/18/determining-if-an-object-is-empty-with-underscore-lo-dash/
Answer from Armen Zakaryan on Stack Overflow_.isEmpty(obj, true)
var obj = {
'firstName': undefined
, 'lastName' : undefined
};
console.log(_.isEmpty(obj)); // false
console.log(_.isEmpty({})); // true
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.js"></script>
Run code snippetEdit code snippet Hide Results Copy to answer Expand
Please, see http://www.ericfeminella.com/blog/2012/08/18/determining-if-an-object-is-empty-with-underscore-lo-dash/
Your example object is not empty so instead perhaps you want to test if all properties are undefined
let o = {foo: undefined};
!_.values(o).some(x => x !== undefined); // true
Why should I use Lodash's isEmpty method in my React project?
What is the fastest way to check if an object is empty in React?
Are there any performance considerations when using JSON.stringify() to check if an object is empty?
Hi everyone,
I very often find myself writing something like
if( Object.keys(someObject).length > 0 ) {
//do some wild stuff
}
To check if a basic object is empty or not i.e. not {} there must be a beautiful way.
I know lodash and jQuery have their solutions, but I don't want to import libraries for a single method, so I'm about to write a function to use across a whole project, but before I do that I want to know I'm not doing something really stupid that ES6/ES7/ES8 can do that I'm just not aware of.
edit solution courtesy of u/vestedfox
Import https://www.npmjs.com/package/lodash.isequal
Total weight added to project after compilation: 355 bytes
Steps to achieve this.
npm i --save lodash.isequal
then somewhere in your code
const isEqual = require('lodash.isequal');
If you're using VueJS and don't want to have to include this in every component and don't want to pollute your global namespace you can do this in app.js
const isEqual = require('lodash.isequal');
Vue.mixin({
methods: {
isEqual: isEqual
}
});Then in your components you can simply write.
if( this.isEqual(someObject, {})) {
console.log('This object has properties');
}As per the lodash documentation here:
Array-like values such as arguments objects, arrays, buffers, strings, or jQuery-like collections are considered empty if they have a length of 0. Similarly, maps and sets are considered empty if they have a size of 0.
[{}].length happens to be 1. A cabbage-in-a-box, if you will. An array with one empty object. Hence, isEmpty evaluates to false. [].length, on the other hand, equals 0.
There might be a handful of other cases you want to cover, like empty array, or an array of two empty objects, etc. Variations on the following should do what you need...
let array = [{}];
// contains any empty object
console.log(_.some(array, _.isEmpty))
// contains only empty objects
console.log(_.every(array, _.isEmpty))
// contains exactly one empty object
console.log(_.every(array, _.isEmpty) && array.length == 1)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.core.js"></script>
You can use a for…in loop with an Object.hasOwn (ECMA 2022+) test to check whether an object has any own properties:
function isEmpty(obj) {
for (const prop in obj) {
if (Object.hasOwn(obj, prop)) {
return false;
}
}
return true;
}
If you also need to distinguish {}-like empty objects from other objects with no own properties (e.g. Dates), you can do various (and unfortunately need-specific) type checks:
function isEmptyObject(value) {
if (value == null) {
// null or undefined
return false;
}
if (typeof value !== 'object') {
// boolean, number, string, function, etc.
return false;
}
const proto = Object.getPrototypeOf(value);
// consider `Object.create(null)`, commonly used as a safe map
// before `Map` support, an empty object as well as `{}`
if (proto !== null && proto !== Object.prototype) {
return false;
}
return isEmpty(value);
}
Note that comparing against Object.prototype like in this example will fail to recognize cross-realm objects.
Do not use Object.keys(obj).length. It is O(N) complexity because it creates an array containing all the property names only to get the length of that array. Iterating over the object accomplishes the same goal but is O(1).
For compatibility with JavaScript engines that don’t support ES 2022+, const can be replaced with var and Object.hasOwn with Object.prototype.hasOwnProperty.call:
function isEmpty(obj) {
for (var prop in obj) {
if (Object.prototype.hasOwnProperty.call(obj, prop)) {
return false;
}
}
return true
}
Many popular libraries also provide functions to check for empty objects:
jQuery:
jQuery.isEmptyObject({}); // true
lodash:
_.isEmpty({}); // true
Underscore:
_.isEmpty({}); // true
Hoek:
Hoek.deepEqual({}, {}); // true
ExtJS:
Ext.Object.isEmpty({}); // true
AngularJS (version 1):
angular.equals({}, {}); // true
Ramda:
R.isEmpty({}); // true
If ECMAScript 5 support is available, you can use Object.keys():
function isEmpty(obj) {
return Object.keys(obj).length === 0;
}
For ES3 and older, there's no easy way to do this. You'll have to loop over the properties explicitly:
function isEmpty(obj) {
for(var prop in obj) {
if(obj.hasOwnProperty(prop))
return false;
}
return true;
}
In lodash, you can use _.some
_.some(props.something, _.isEmpty)
You can use lodash _.every and check if _.values are _.isEmpty
const profile = {
name: 'John',
age: ''
};
const emptyProfile = _.values(profile).every(_.isEmpty);
console.log(emptyProfile); // returns false