How to Find Dictionary Length in TypeScript?

Do you need to get the dictionary length in Typescript? In this Typescript tutorial, I have explained how to find dictionary length in Typescript using various methods.

When working with dictionaries in TypeScript, a common requirement is to determine the number of key-value pairs present, commonly referred to as the dictionary’s length. Here are various methods to get the length of a dictionary in TypeScript.

Use Object.keys() to get Typescript Dictionary Length

To get dictionary length in Typescript, you can use the object.keys() method. This is one of the simplest and most common methods to get the length of a dictionary in TypeScript.

The Object.keys() function retrieves an array of a given object’s own enumerable property names, which you can then use to determine the number of properties.

Here is a complete example.

// Define a dictionary
let myDictionary: { [key: string]: any } = {
  "firstKey": 1,
  "secondKey": 2,
  "thirdKey": 3
};

// Get the length of the dictionary
let dictionaryLength: number = Object.keys(myDictionary).length;

console.log(dictionaryLength);

Output:

3

Here, you can check the screenshot below; it gives the output after I ran the code using the VS Code editor.

typescript get dictionary length

Get Typescript dictionary length using a For-In Loop

Let us check another approach to get the length of a dictionary by iterating over it using a for-in loop in Typescript.

Here is the complete code:

let myDictionary: { [key: string]: any } = {
    "firstKey": 1,
    "secondKey": 2,
    "thirdKey": 3
  };
  let length = 0;
  // Iterate over the dictionary
  for (let key in myDictionary) {
    if (myDictionary.hasOwnProperty(key)) {
      length++;
    }
  }
  
  console.log(length); 

Once you run the code using Visual Studio Code, you can see the output as 3, like the screenshot below.

typescript length of dictionary

Get the length of a dictionary in Typescript using Object.entries()

If you need both keys and values during your length calculation, Object.entries() is an appropriate function in Typescript. It returns an array of a given object’s own enumerable string-keyed property [key, value] pairs.

let myDictionary: { [key: string]: any } = {
  "firstKey": 1,
  "secondKey": 2,
  "thirdKey": 3
};

// Get the length by counting the entry pairs
let length = Object.entries(myDictionary).length;

console.log(length);

Here also, it will give the output as 3 after you run the Typescript code using an editor.

Find the length of a dictionary in Typescript using a Custom Function

As a Typescript developer, you can also write a custom function to find the length of a dictionary in Typescript. Here is the complete code.

interface Dictionary<T> {
  [key: string]: T;
}

function getDictionaryLength<T>(dict: Dictionary<T>): number {
  return Object.keys(dict).length;
}

let myDictionary: Dictionary<number> = {
  "firstKey": 1,
  "secondKey": 2,
  "thirdKey": 3
};

console.log(getDictionaryLength(myDictionary));

Once you run the code using Visual Studio code, the output will come as 3.

A custom function like getDictionaryLength can provide more readability and reusability, especially when dealing with dictionaries throughout your TypeScript application.

Conclusion

In TypeScript, determining the length of a dictionary is a straightforward process that can be accomplished using various methods such as Object.keys(), a for-in loop, Object.entries(), or a custom function. This is how to get the length of a dictionary in Typescript using various methods.

You may like the following tutorials:

>