![]() |
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 of examples and outputs. Table of Content Using Dot NotationDot notation is used in TypeScript to directly access the value associated with the “name” key within the object obj. The result is stored in the variable res, which consists of the value “GeeksforGeeks,” and is then printed to the console. Syntax: let value = obj.key Example: The below example uses Dot Notation to Get an Object Value By Key in TypeScript.
Output: "GeeksforGeeks" Using Bracket NotationIn this approach, we are using bracket notation ([]) in TypeScript to access the value associated with the “category” key within the object obj. The result is stored in the variable res, which consists of the value “Programming,” and is then printed to the console. Syntax: let value = obj[key]; Example: The below example uses Bracket Notation to Get an Object Value By Key in TypeScript.
Output: "Programming" Using Optional ChainingIn this approach, we are using optional chaining in TypeScript, we access the value associated with the “language” key within the object obj. The result, is then converted to uppercase which is stored in the variable res, which contains “TYPESCRIPT” and is then printed to the console. Syntax: object?.property Example: The below example uses Optional Chaining to Get an Object Value By Key in TypeScript.
Output: "TYPESCRIPT" Using Object.hasOwnProperty() MethodThis method checks if an object contains a specified property as its own property, and if it does, retrieves the corresponding value. It’s particularly useful when you want to ensure that the property exists before accessing its value. Syntax: if (obj.hasOwnProperty(key)) { Example: In this example we retrieves the value associated with the key ‘category’ from the object obj and logs it to the console.
Output: Programming Using Object.entries() and Array.find()In TypeScript, you can use Object.entries() to get an array of key-value pairs from an object. Then, you can use array methods like Array.find() to find the value associated with a specific key. Example: In this example, we’ll use Object.entries() and Array.find() to get the value associated with the key “category” from the object obj.
Output Programming Using Object.keys() MethodIn TypeScript, you can use the Syntax: Object.keys(obj).forEach(key => { Example: In this example, we’ll use Object.keys() to get the value associated with the key “category” from the object obj.
Output: Programming Using TypeScript’s in OperatorAnother approach to access an object value by key in TypeScript is by using the in operator. This method checks if a property exists within the object before accessing its value. It’s particularly useful for ensuring that the property exists and can handle dynamic keys effectively. Example: In this example, we use the in operator to check if the key exists in the object and then access its value.
Output: Programming |
Reffered: https://www.geeksforgeeks.org
JavaScript |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 13 |