How to use Object.keys() Method In Typescript

In TypeScript, you can use the Object.keys() method to get an array of the object’s own enumerable property names. Then, you can iterate over this array to find and access the value associated with a specific key.

Syntax:

Object.keys(obj).forEach(key => {
if (key === specificKey) {
let value = obj[key];
}
});

Example: In this example, we’ll use Object.keys() to get the value associated with the key “category” from the object obj.

JavaScript
let obj = { name: "w3wiki", category: "Programming", language: "TypeScript" };
let specificKey = "category";
let res: string | undefined;

Object.keys(obj).forEach(key => {
    if (key === specificKey) {
        res = obj[key];
    }
});
console.log(res);

Output:

Programming


How to Get an Object Value By Key in TypeScript

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 Notation
  • Using Bracket Notation
  • Using Optional Chaining
  • Using Object.hasOwnProperty() Method
  • Using Object.entries() and Array.find()
  • Using Object.keys() Method

Similar Reads

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

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

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

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

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

Using Object.keys() Method

In TypeScript, you can use the Object.keys() method to get an array of the object’s own enumerable property names. Then, you can iterate over this array to find and access the value associated with a specific key....