Hi Friends đź‘‹,

Welcome to Infinitbility ❤️!

Today, we will learn how to get value from an object by key in typescript, here we will use some basic syntax to get value from an object.

Here, we will see

  1. Access object value using the dot method
  2. Access object value using bracket method ( we can also use it for dynamic value access )

In the code below, we create a sample object variable to store sample dog data.

after creation, I use variables to access the object values.

Let’s dive into code…

// create sample object variable
const animals = {
  id: 1,
  name: "dog",
  age: 5
};

// access value by dot syntax
animals.id;

// access value by bracket ( we can also use it for dynamic value access )
animals["name"]

When you run the above code, you will get the id and name keys value. For now, let’s check the output.

TypeScript, get value from object by key example
TypeScript, get value from object by key example

All the best đź‘Ť.