I was doing it wrong for years LOL. lodash/underscore _.isEmpty() actually works differently then I expected.
isEmpty returns true when a number is passed in
How to check if object is empty using lodash _isEmpty?
lodash isEmpty and has method vs simple check
Why should I use Lodash's isEmpty method in my React project?
Are there any performance considerations when using JSON.stringify() to check if an object is empty?
Can JSON.stringify() handle circular references when checking for empty objects?
Videos
I wrongly assumed that (as name suggest) _.isEmpty checks if variable is somehow empty, like undefined, null, empty string, array etc. But I just noticed that _.isEmpty(123) will return true, same as _.isEmpty(true) or _.isEmpty(false).
Documentation explains why:
A value is considered empty unless it is an arguments object, array, string, or jQuery-like collection with a length greater than 0 or an object with own enumerable properties.
» npm install @types/lodash.isempty
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>
if(user) will pass for empty Object/Array, but they are empty and should be rejected.
Also if(user) will fail for values like 0 or false which are totally valid values.
Using isEmpty() will take care of such values. Also, it makes code more readable.
Point to note is isEmpty(1) will return true as 1 is a primitive value and not a data structure and hence should return true.
This has been stated in Docs:
Checks if value is an empty object, collection, map, or set.
Also as per docs,
Objects are considered empty if they have no own enumerable string keyed properties.
So if you have an object which does not have non-enumerable properties, its considered as empty. In the below example, foo is a part of object o and is accessible using o.foo but since its non-enumerable, its considered as empty as even for..in would ignore it.
var o = Object.create(null);
Object.defineProperty(o, "foo", {
enumerable: false,
value: "Hello World"
})
Object.defineProperty(o, "bar", {
enumerable: false,
value: "Testing 123"
});
console.log(o)
for (var k in o) {
console.log(k)
}
console.log(o.foo)
console.log(_.isEmpty(o))
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.min.js"></script>
Note: This does not mean you should use lodash for such purpose. You can write your own isEmpty function.
Following is something that I use:
This will return true for following cases:
{},[],"",undefined,null, object in above snippet(no enumerable property)
function isEmpty(value){
return value === undefined ||
value === null ||
(typeof value === "object" && Object.keys(value).length === 0) ||
(typeof value === "string" && value.trim().length === 0)
}
Simple and elegant function for checking the values are empty or not
function isEmpty(value) {
const type = typeof value;
if ((value !== null && type === 'object') || type === 'function') {
const props = Object.keys(value);
if (props.length === 0 || props.size === 0) {
return true;
}
}
return !value;
}
Testing the above method
It will return 'true' for all cases below
console.log(isEmtpy(null))
console.log(isEmtpy(0))
console.log(isEmtpy({}))
console.log(isEmtpy(new Set())
console.log(isEmtpy(Object.create(null))
console.log(isEmtpy(''))
console.log(isEmtpy(() => {}))
console.log(isEmtpy(() => [])
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');
}