Open In App

Find the Length of a Dictionary in TypeScript

Last Updated : 04 Sep, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

In TypeScript, to find the length of a dictionary, there is no built-in property for objects that determines the length of the dictionary. The length of the input dictionary can be retrieved by counting the pairs of key values in it.

There are several approaches available in TypeScript for finding the length of a dictionary which are as follows:

Using Object.keys() function

In this approach, we are using the Object.keys() function in TypeScript to extract an array of keys from the dictionary (dict). By using the length property to this array, we find the count of keys which is the length of the dictionary. The output res variable stores the number of key-value pairs, and then prints to the console.

Syntax:

Object.keys(obj: object): string[]; 

Example: The below example uses the Object.keys() function to find the length of a dictionary in TypeScript.

JavaScript
const dict: Record<string, string> = {
    'data structures': 'algorithms',
    'programming languages': 'python',
    'computer science': 'geeksforgeeks',
};
const res: number = Object.keys(dict).length;
console.log(res);

Output:

3

Using for...in Loop

In this approach, we are using a for...in loop in TypeScript to iterate through the enumerable properties (keys) of the dictionary (dict). For each iteration, the loop checks if the property is directly owned by the object using hasOwnProperty to exclude properties inherited from the prototype chain. If true, the res variable is incremented, and then final count is then printed to the console.

Syntax:

for (variable in object) {
 // code 
}

Example: The below example uses the for...in loop to find the length of a dictionary in TypeScript.

JavaScript
const dict: Record<string, string> = {
    'data structures': 'algorithms',
    'programming languages': 'python',
    'computer science': 'geeksforgeeks',
};
let res: number = 0;
for (const key in dict) {
    if (dict.hasOwnProperty(key)) {
        res++;
    }
}
console.log(res);

Output:

3

Using reduce() function

In this approach, the reduce() function is used on the array of keys obtained using Object.keys() to accumulate and count each key, starting with an initial value of 0. The output res variable stores the length of the dictionary, and it is then printed to the console.

Syntax:

array.reduce(callback: (accumulator: T, currentValue: T, currentIndex: number, array: T[]) => T, 
initialValue?: T): T; 

Example: The below example uses the reduce function to find the length of a dictionary in TypeScript.

JavaScript
const dict: Record<string, string> = {
    'data structures': 'algorithms',
    'programming languages': 'python',
    'computer science': 'geeksforgeeks',
};
const res: number = Object
    .keys(dict).
    reduce((acc) => acc + 1, 0);
console.log(res);

Output:

3

Using Object.entries()

In this approach, we utilize the Object.entries() function to retrieve an array of key-value pairs from the dictionary (dict). We then determine the length of this array, which corresponds to the number of entries in the dictionary.

Syntax:

Object.entries(obj: object): [string, any][];

Example: This TypeScript code defines a dictionary `dict`, then calculates its length using `Object.entries()` and retrieves the number of key-value pairs. Finally, it logs the result.

JavaScript
const dict: Record<string, string> = {
    'data structures': 'algorithms',
    'programming languages': 'python',
    'computer science': 'geeksforgeeks',
};
const res: number = Object.entries(dict).length;
console.log(res); // Output: 3

Output:

3

Using Object.values() Function

In this approach, we utilize the Object.values() function to extract an array of values from the dictionary (dict). By applying the length property to this array, we can determine the number of key-value pairs in the dictionary. The result is stored in the res variable and then printed to the console.

Example: The example below demonstrates how to use the Object.values() function to find the length of a dictionary in TypeScript.

JavaScript
// Define a dictionary with different types of values
const dict: Record<string, any> = {
    'name': 'Alice',
    'age': 30,
    'isStudent': false,
    'subjects': ['Math', 'Science'],
    'address': {
        city: 'Wonderland',
        zipCode: '12345'
    }
};

// Calculate the length of the dictionary using Object.values()
const lengthOfDict: number = Object.values(dict).length;

// Output the result
console.log(`The length of the dictionary is: ${lengthOfDict}`); 

Output:

The length of the dictionary is: 5



Next Article

Similar Reads