How to check if a key exists in a dictionary in typescript?

Recently, one of my team members was searching for “Typescript dictionary check if key exists”. Then, after checking in detail, I thought I would write this tutorial on how to check if a key exists in a dictionary in typescript.

To check if a key exists in a dictionary in Typescript, you can use different methods like, the “in” Operator, Using “hasOwnProperty” Method, Using “undefined” Check, and using Using the Map Object. The simple way of doing this is by using the “in” operator like if (keyToCheck in dictionary).

Typescript dictionary check if key exists

Now, let us check out various methods of how to check if a key exists in a dictionary in typescript.

1. Check if a key exists in a dictionary in Typescript using the “in” Operator

One of the simplest ways to check if a key exists in a dictionary in TypeScript is by using the in operator. This operator checks whether the object has the specified property as its own property or inherited.

Here is a complete example.

interface IDictionary {
  [key: string]: any;
}

const dictionary: IDictionary = {
  "firstKey": "value1",
  "secondKey": "value2"
};

const keyToCheck: string = "firstKey";

if (keyToCheck in dictionary) {
  console.log(`The key "${keyToCheck}" exists in the dictionary.`);
} else {
  console.log(`The key "${keyToCheck}" does not exist in the dictionary.`);
}

Here, you can see, that I ran the code using Visual Studio Code, and it gave the output as shown in the screenshot below:

The key "firstKey" exists in the dictionary.
check if a key exists in a dictionary in typescript

2. Check if a key exists in a dictionary in Typescript using the Map Object

The Map object in TypeScript provides a standard and efficient way to manage collections of keyed data items. It has a built-in has method, which is explicitly designed to check the presence of a key in a Typescript dictionary.

See also  Typescript split string by dot

Here is a complete example:

const map = new Map<string, any>();
map.set("firstKey", "value1");
map.set("secondKey", "value2");

const keyToCheck: string = "firstKey";

if (map.has(keyToCheck)) {
  console.log(`The key "${keyToCheck}" exists in the map.`);
} else {
  console.log(`The key "${keyToCheck}" does not exist in the map.`);
}

After I run the code using Visual Studio Code, you can see the output in the screenshot below:

how to check if a key exists in a dictionary in typescript

3. Check if a key exists in a dictionary in Typescript using “hasOwnProperty” method

Now, let us see how to check if a key exists in a Typescript dictionary using the “hasOwnProperty” method. You can use the hasOwnProperty method, which checks if the object has the specified property as a direct property of that object.

Here is the complete code:

const dictionary: { [key: string]: any } = {
  "firstKey": "value1",
  "secondKey": "value2"
};

const keyToCheck: string = "thirdKey";

if (dictionary.hasOwnProperty(keyToCheck)) {
  console.log(`The key "${keyToCheck}" exists in the dictionary.`);
} else {
  console.log(`The key "${keyToCheck}" does not exist in the dictionary.`);
}

You can see the output in the screenshot below after I run the code using Visual Studio Code.

The key "thirdKey" does not exist in the dictionary.
typescript dictionary check if key exists

4. Check if a key exists in a dictionary in Typescript using “undefined” check

TypeScript, being a superset of JavaScript, allows us to perform a straightforward check for undefined to determine the existence of a key in a dictionary in Typescript. Here is a complete example.

const dictionary: { [key: string]: any } = {
  "firstKey": "value1",
  "secondKey": "value2"
};

const keyToCheck: string = "secondKey";

if (dictionary[keyToCheck] !== undefined) {
  console.log(`The key "${keyToCheck}" exists in the dictionary.`);
} else {
  console.log(`The key "${keyToCheck}" does not exist in the dictionary.`);
}

Conclusion

I hope now you find an answer for “typescript dictionary check if key exists”. In this tutorial, I have explained in detail how to check if a key exists in a dictionary in typescript using different methods:

  • using the “in” Operator
  • using the Map Object
  • using “hasOwnProperty” method
  • using “undefined” Check
See also  How to Convert Typescript Array to String With Separator [Using 4 Methods]

You may also like:

>