Open In App

How to Convert a Dictionary to an Array in TypeScript ?

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

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:

Using Object.keys method

A common approach to transform a TypeScript dictionary into an array involves utilizing Object.keys(), which provides an array comprising the enumerable property name of the given object.

Syntax:

Object.keys(obj);  // It will return the array of all keys.

Example: The below code uses `keys()` method to convert the dictionary into an array using TypeScript.

Javascript
// Creating a dictionary object
const dictionary = {
    name: "Hritik",
    email: "gfg@gmail.com",
    isActive: true,
    mobile: 9876543210,
}

// Fetch the all keys from the dictionary as an array
const allKeys = Object.keys(dictionary);

console.log("Here, We have array of all keys:");
console.log(allKeys);

Output
Here, We have array of all keys:
[ 'name', 'email', 'isActive', 'mobile' ]

Using Object.values method

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.

Javascript
// 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 ]

Using Object.entries method

When both keys and values are required in the form of an array, the Object.entries() method comes in handy. This method fetches both keys and values from the dictionary, allowing us to convert them into an array of objects or an array of arrays based on our specific needs.

Syntax:

Object.entries(obj)
// The method returns an array of array containing key, value pairs

Example: The below code uses `entries()` methods to convert the dictionary into an array using TypeScript.

Javascript
// Creating a dictionary object
const dictionary = {
    name: "Hritik",
    email: "gfg@gmail.com",
    isActive: true,
    mobile: 9876543210,
}

// Now, converting the dictionary object into an array
const arrOfArr = Object.entries(dictionary);

console.log("Converting an object into an array of arrays");
console.log(arrOfArr);

Output
Converting an object into an array of arrays
[
  [ 'name', 'Hritik' ],
  [ 'email', 'gfg@gmail.com' ],
  [ 'isActive', true ],
  [ 'mobile', 9876543210 ]
]

Using Object.getOwnPropertyNames

In this approach we are using `Object.getOwnPropertyNames()` to retrieve an array containing all property names of an object. It includes non-enumerable properties and returns them as an array for further processing or analysis.

Syntax:

Object.getOwnPropertyNames(obj);  // Returns an array of all properties' names

Example: In this example we creates a dictionary object and retrieves all property names using Object.getOwnPropertyNames(), storing them in an array. It then logs the array of property names.

JavaScript
// Creating a dictionary object
const dictionary = {
    name: "Nikunj",
    email: "gfg@gmail.com",
    isActive: true,
    mobile: 9876543210,
}

// Fetch all property names from the dictionary as an array
const allProperties = Object.getOwnPropertyNames(dictionary);

console.log("Here, We have an array of all property names:");
console.log(allProperties);

Output:

"Here, We have an array of all property names:" 
["name", "email", "isActive", "mobile"] 


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads