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
- Access object value using the dot method
- 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.
All the best 👍.