How to use Object Methods In Typescript
TypeScript provides Object methods like Object.keys() and Object.values() which can be used to access dictionary values by key.
Syntax:
let keys = Object.keys(dictionary);
let values = Object.values(dictionary);
Example:
interface Dictionary {
name: string;
desc: string;
est: number;
}
let dictionary: Dictionary = {
name: "w3wiki",
desc: "A Computer Science Portal",
est: 2009
};
let keys = Object.keys(dictionary);
let values = Object.values(dictionary);
console.log(keys);
console.log(values);
Output:
["name", "desc", "est"]
["w3wiki", "A Computer Science Portal", 2009]
How to Access Dictionary Value by Key in TypeScript ?
In TypeScript, dictionaries are often represented as the objects where keys are associated with the specific values. These TypeScript dictionaries are very similar to JavaScript objects and are used wherever data needs to be stored in key and value form. You can use the below method to access the dictionary values by key.
Table of Content
- Using Square Bracket Notation
- Using Dot Notation
- Using Object Methods