How to use Object.values method In Typescript
A common approach to transform a TypeScript dictionary into an array involves utilizing Object.entries(), which provides an array comprising the values of the given object.
Syntax:
Object.values(obj); // It will return the array of all values.
Example: The below code uses `values()` method to convert the dictionary into an array using TypeScript.
// Creating a dictionary object
const dictionary = {
name: "Hritik",
email: "gfg@gmail.com",
isActive: true,
mobile: 9876543210,
}
// We can fetch all the values from the dictionary as an array
const allValues = Object.values(dictionary);
console.log("Here, We have array of all values:");
console.log(allValues);
Output
Here, We have array of all values: [ 'Hritik', 'gfg@gmail.com', true, 9876543210 ]
How to Convert a Dictionary to an Array in TypeScript ?
In TypeScript, converting a dictionary(an object) into an Array can be useful in scenarios where you need to work with the values in a more iterable and flexible manner.
These are the following approaches:
Table of Content
- Using Object.keys method
- Using Object.values method
- Using Object.entries method
- Using Object.getOwnPropertyNames
Contact Us