Open In App

Find the Length of a Dictionary in TypeScript

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.

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.

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.

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.

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
Article Tags :