Objects are one of the best features of JavaScript because they make storing complex data in memory much easier. Each object in JavaScript is a set of key-value pairs which is really special because you can not find this kind of data structure in many programming languages. In this post we will the different ways available to check if a JavaScript object contains any keys and values. We won’t be using only JavaScript but also some external libraries.
JavaScript is one of the most popular scripting languages, so it provides many ways to make any operation you want. Today, we will go through all the ways that JS provides to check if an Object is empty or it contains some keys and values.
The first method that would help us find out if an object is empty or not is Object.keys(). This is a static method that takes an object as argument and returns an array containing the keys or the properties of that object.
Using method we will have the array containing all properties of an object, and then we can check if the array’s length is equal to zero or not. If the length is zero then the object is empty, otherwise the object have some keys and values.
The following function objectContainsKeys() takes an object as argument and returns a boolean, so if the object contain one or more key-value pairs it returns true, otherwise it returns false.
function objectContainsKeys(obj) {
return Object.keys(obj).length > 0;
}
console.log(objectContainsKeys({ name: 'David' })); // true
Using this Object.keys() method is the easiest and the best way to check if a object has any keys.
This method is actually very similar to the Object.keys() method, and the only difference between both of them is that the Object.keys() method returns an array containing the keys of the object. However, the Object.values() return an array containing all the values of a given object.
The following snippet of code represents a function that returns true if the object contains any key-value pairs and false if the object doesn’t contain anything.
function objectContainsKeys(obj) {
return Object.values(obj).length > 0;
}
console.log(objectContainsKeys({ name: 'David' })); // true
This method is also similar to the previous two methods, but with some differences. The Object.entries() method returns an array containing a set of arrays and each one of them contains two elements key and value, so if we check if the resulted array whether it’s empty or not we can tell in the object contains.
The following snippet of code represents a function that return if the object contains any elements or not using the Object.entries() method.
function objectContainsKeys(obj) {
return Object.entries(obj).length > 0;
}
console.log(objectContainsKeys({ name: 'David' })); // true
Another great method that we can use to check whether an object is empty or not is the hasOwnProperty() method, which is a little bit different of all the methods we talked about previously.
This method will take a property of an object as argument and then it returns a boolean based on if the object contains that property. We can write some code and check whether the object is empty or not.
function objectContainsKeys(obj) {
for(let prop in obj) {
if(obj.hasOwnProperty(prop)) {
return true;
}
}
return false;
}
JavaScript has a lot of external packages that can be used to build some features in our web app. There are some JavaScript libraries that can help us find out if a given object has some keys and values or not.
Lodash and underscore are two libraries that provide functions to check if an object is empty or not. We can add these packages either by importing them via CDN.
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.21/lodash.min.js" integrity="sha512-WFN04846sdKMIP5LKNphMaWzU7YpMyCU245etK3g/2ARYbPK9Ub18eG+ljU96qKRCWh+quCY7yefSmlkQw1ANQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.13.4/underscore-umd-min.min.js" integrity="sha512-5B2sl+/Nbe4Q1KY3csUeHRxjTPJJvCtGfNnIWVSShvIkeFhfRa54cGRkovghfyzoDNaj5cyvAGNfCEagZGikVg==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
Or we can install them using the node package manager NPM and then use them with the node js require() function.
npm install lodash
npm install underscore
The function provided by both packages to check whether an object is empty or not called isEmpty(). Check the following snippet of code
const lodash = require('lodash');
const underscore = require('underscore');
console.log(!lodash.isEmpty({ name: 'David' })); //false
console.log(!underscore.isEmpty({ name: 'David' })); // false
JavaSript is a very rich language that provides a lot of ways to implement any feature or functionality. In this article, we went through multiple different ways on how we can check weather an object contains any keys or values.
I hope you enjoyed reading this article and Thank you.