![]() |
In TypeScript, a dictionary consists of objects that store data in the key-value pairs, and allow retrieval of values based on the specified keys. We can get value from a dictionary for the specific keys using various approaches. Table of Content Using Dot NotationIn this approach, we will use the dot notation to directly access the value associated with a key in the dictionary and print the resulting and retrieved value in the console. Syntax:const value = obj.propertyName; Example: The below example uses dot notation to get value from a dictionary in TypeScript.
Output: GeeksforGeeks, A Computer Science Portal, was establised in: 2009 Using Bracket NotationIn this approach, we use bracket notation to access the value associated with the key in the dictionary object. The retrieved value is logged to the console using console.log(). Syntax:const value = obj['propertyName']; Example: The below example uses bracket notation to get value from a dictionary in TypeScript.
Output: GeeksforGeeks, A Computer Science Portal, was establised in: 2009 Using a FunctionThe function approach involves creating a reusable function that takes a dictionary object and a key as parameters, returning the corresponding value. This method ensures code modularity and flexibility, allowing easy retrieval of values from dictionaries in TypeScript. Syntaxconst getValue = <T extends Record<string, any>>( Example
Output: Name: GeeksforGeeks |
Reffered: https://www.geeksforgeeks.org
JavaScript |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 13 |