Open In App

How to Get an Object Value By Key in TypeScript

Last Updated : 12 Apr, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

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.

Using Dot Notation

Dot 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.

Javascript
const obj: { [key: string]: string } = {
    name: "GeeksforGeeks",
    category: "Programming",
    language: "TypeScript",
};
const res: string = obj.name;
console.log(res); 

Output:

"GeeksforGeeks" 

Using Bracket Notation

In 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.

Javascript
const obj: { [key: string]: string } = {
    name: "GeeksforGeeks",
    category: "Programming",
    language: "TypeScript",
};
const res: string = obj['category'];
console.log(res); 

Output:

"Programming" 

Using Optional Chaining

In 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
object?.method()
object?.[expression]

Example: The below example uses Optional Chaining to Get an Object Value By Key in TypeScript.

Javascript
const obj: { name?: string; category?: string; language?: string } = {
    name: "GeeksforGeeks",
    category: "Programming",
    language: "TypeScript",
};
const res: string | undefined = obj.language?.toUpperCase();
console.log(res);

Output:

"TYPESCRIPT" 

Using Object.hasOwnProperty() Method

This 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)) {
    let value = obj[key];
}

Example: In this example we retrieves the value associated with the key ‘category’ from the object obj and logs it to the console.

JavaScript
const obj: { [key: string]: string } = {
    name: "GeeksforGeeks",
    category: "Programming",
    language: "TypeScript",
};

if (obj.hasOwnProperty('category')) {
    const res: string = obj['category'];
    console.log(res);
}

Output:

Programming


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads