Horje
Check if an Object is Empty in TypeScript

In TypeScript, determining whether an object is empty involves checking if the object has any properties. This can be achieved through various built-in methods and loops. In this article, we will explore three different approaches to check if an object is empty in TypeScript.

Using Object.keys() function

In this approach, we are using the Object.keys() function in TypeScript to check if an object is empty. The condition Object.keys(obj).length === 0 evaluates to true when the object (obj) has no properties, which indicates that the object is empty

Syntax:

Object.keys(obj)

Example: The below example uses Object.keys() function to check if an Object is empty in TypeScript.

TypeScript
function approach1Fn(obj: Record<string, any>): boolean {
  return Object.keys(obj).length === 0;
}
const obj: Record<string, any> = {};
const res: boolean = approach1Fn(obj);
console.log(res);

Output:

true

Using for…in Loop

In this approach, we are using a for…in loop in TypeScript to iterate through the properties of the object (obj). The loop checks if any property is present, and if it is present, it returns false, indicating that the object is not empty. If no properties are found, the function returns true, indicating that the object is empty.

Syntax:

for (const variable in object) {
  // code
}

Example: The below example uses for…in Loop to check if an Object is empty in TypeScript.

TypeScript
function approach2Fn(obj: Record<string, any>): boolean {
    for (const key in obj) {
        if (obj.hasOwnProperty(key)) {
            return false;
        }
    }
    return true;
}
const obj: Record<string, any> = {};
const res: boolean = approach2Fn(obj);
console.log(res); 

Output:

true

Using JSON.stringify() function

In this approach, we are using the JSON.stringify() function in TypeScript to convert the object (obj) into a JSON string. The equality check JSON.stringify(obj) === ‘{}’ checks if the resulting string represents an empty object. If true, it indicates that the original object is empty, printing in the function returning true as output.

Syntax:

JSON.stringify(value, replacer?, space?)

Example: The below example uses JSON.stringify() function to check if an Object is empty in TypeScript.

TypeScript
function approach3Fn(obj: Record<string, any>): boolean {
  return JSON.stringify(obj) === '{}';
}
const obj: Record<string, any> = {}; 
const res: boolean = approach3Fn(obj);
console.log(res);

Output:

true



Reffered: https://www.geeksforgeeks.org


JavaScript

Related
JavaScript Program to Check Whether a Number is Perfect Square JavaScript Program to Check Whether a Number is Perfect Square
What is the use of as const assertion in TypeScript? What is the use of as const assertion in TypeScript?
How to Check Types in Typescript? How to Check Types in Typescript?
JavaScript Program to Find the Area of a Square JavaScript Program to Find the Area of a Square
JavaScript Program to Print Double Sided Stair-Case Pattern JavaScript Program to Print Double Sided Stair-Case Pattern

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
15