How to Check if all Enum Values Exist in an Object in TypeScript ?
Last Updated :
16 Apr, 2024
To check if all values of an enum exist in an object in TypeScript, iterate through the enum values and verify their presence in the object. This ensures that the object encompasses all possible enum values, confirming completeness and adherence to the enum definition. There are various methods to check if all the enum values exist in an object which are as follows:
Using Object.keys() and includes()
In this approach, To check if all enum values exist in an object, use TypeScript's Object.keys() and includes(). Iterate through enum keys, then verify their presence in the object. This ensures the object incorporates all enum values.
Syntax:
const allEnumValuesExist = Object.keys(EnumType).every(key =>
Object.values(yourObject).includes(EnumType[key])
);
Example: In this example, checks if all values of the Branch enum exist in the department object using Object.keys() and includes(). In this case, it returns true since the only enum value matches the department's branch.
JavaScript
enum Branch {
CivilEngineering = "Civil Engineering",
}
const department = {
branch: "Civil Engineering",
manager: "Mukesh",
location: "Building A",
};
const allEnumValuesExist =
Object.keys(Branch).every(key =>
{
return Object.values(department).includes(Branch[key])
});
console.log(allEnumValuesExist);
Output:
true
Using Object.values() and every()
In this approach, TypeScript utilizes Object.values() and every() to confirm if all values of an enum, such as Branch, are present in an object, e.g., department. This concise method iterates through enum values, checking their inclusion in the object, ensuring completeness and adherence to predefined enum values.
Syntax:
const allEnumValuesExist = Object.values(EnumType).every(value =>
Object.values(yourObject).includes(value)
);
Example: This TypeScript example checks if all values of the Branch enum exist in the department object using Object.values() and every(). It outputs true as the object incorporates all defined enum values.
JavaScript
enum Branch {
CivilEngineering = "Civil Engineering",
MechanicalEngineering = "Mechanical Engineering",
ElectricalEngineering = "Electrical Engineering",
}
const department = {
branch: "Civil Engineering",
manager: "Nikita",
location: "Building A",
};
// Check if all enum values exist in the object
const allEnumValuesExist =
Object.values(Branch).every(value =>
{
return Object.values(department).includes(value)
});
console.log(allEnumValuesExist);
Output:
false
Using Object.entries() and every()
In this method, TypeScript's Object.entries() and every() functions are employed to ensure that all values of an enum, such as Branch, are present in an object, e.g., department. This technique iterates through enum key-value pairs, validating their existence in the object, thereby confirming adherence to the predefined enum values.
Syntax:
const allEnumValuesExist = Object.entries(EnumType).every(([key, value]) =>
Object.values(yourObject).includes(value)
);
Example: This TypeScript example verifies if all values of the Branch enum exist in the department object using Object.entries() and every(). It returns true as the object contains all defined enum values.
JavaScript
enum Branch {
CivilEngineering = "Civil Engineering",
MechanicalEngineering = "Mechanical Engineering",
ElectricalEngineering = "Electrical Engineering",
}
const department = {
branch: "Civil Engineering",
manager: "Nikita",
location: "Building A",
};
// Check if all enum values exist in the object
const allEnumValuesExist =
Object.entries(Branch).every(([key, value]) =>
{
return Object.values(department).includes(value)
});
console.log(allEnumValuesExist);
Output:
false
Similar Reads
How to Check if an Object is Empty in TypeScript ? In TypeScript, it's common to encounter scenarios where you need to determine if an object is empty or not. An empty object typically means it contains no properties or all its properties are either undefined or null. Below are the methods to check if an object is empty or not in TypeScript: Table o
3 min read
How to Check if a Key Exists in a Dictionary in TypeScript ? In TypeScript dictionaries are used whenever the data is needed to be stored in key and value form. We often retrieve the data from the dictionaries using an associated key. Therefore it becomes crucial to check whether the key exists in a dictionary or not. We can use the below methods to check if
4 min read
How to Get an Object Value By Key in TypeScript In TypeScript, we can get an object value by key by accessing the specific properties within the objects of the dynamic type. This can be done using Dot Notation, Bracket Notation, and Optional Chaining. In this article, we will explore all these approaches along with their implementation in terms o
5 min read
How to Check if an Array Includes an Object in TypeScript ? In TypeScript, checking if an array includes an object consists of comparing the object's properties within the array elements. We can compare and check this using three different approaches some method, find method, and includes method. There are several ways to check if an array includes an object
3 min read
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.Table of ContentUsing Ob
2 min read