How to Convert an Object to an Array in TypeScript

avatar
Borislav Hadzhiev

Last updated: Feb 27, 2024
2 min

banner

# Convert an object to an Array in TypeScript

There are three ways to convert an object to an array in TypeScript:

  1. Object.keys() - returns an array containing the object's keys.
  2. Object.values() - returns an array containing the object's values.
  3. Object.entries() - returns an array containing key-value pair arrays.
index.ts
const obj = { name: 'Bobby', country: 'Chile' }; const keys = Object.keys(obj); console.log(keys); // ๐Ÿ‘‰๏ธ ['name', 'country'] const values = Object.values(obj); console.log(values); // ๐Ÿ‘‰๏ธ ['Bobby', 'Chile'] const entries = Object.entries(obj); console.log(entries); // ๐Ÿ‘‰๏ธ [['name', 'Bobby'], ['country', 'Chile']]

convert object to array in typescript

The code for this article is available on GitHub

The first example uses the Object.keys method to get an array of the object's keys.

The method returns an array of strings where the keys of the object are in the same order as provided by a for...in loop.

index.js
// ๐Ÿ‘‡๏ธ๏ธ ['one', 'two'] console.log(Object.keys({one: 1, two: 2}));

You can then iterate over the array of keys as follows.

index.ts
const obj = { name: 'Bobby', country: 'Chile' }; const keys = Object.keys(obj) as (keyof typeof obj)[]; console.log(keys); // ๐Ÿ‘‰๏ธ ['name', 'country'] keys.forEach((key) => { // ๐Ÿ‘‡๏ธ name Bobby, country Chile console.log(key, obj[key]); });

# Convert an Object's values to an Array in TypeScript

You can use Object.values method to convert an object's values to an array.

index.ts
const obj = { name: 'Bobby Hadz', country: 'Chile' }; const values = Object.values(obj); console.log(values); // ๐Ÿ‘‰๏ธ ['Bobby Hadz', 'Chile']

convert object values to array in typescript

The code for this article is available on GitHub

Similarly to the keys method, the values are added to the array in the same order as that provided by a for...in loop.

# Convert an Object's entries to an Array in TypeScript

Use the Object.entries method to convert an object to an array of key-value pair arrays.

index.ts
const obj = { name: 'Bobby Hadz', country: 'Chile' }; // ๐Ÿ‘‡๏ธ const entries: [string, string][] const entries = Object.entries(obj); console.log(entries); // ๐Ÿ‘‰๏ธ [['name', 'Bobby Hadz'], ['country', 'Chile']]

convert object entries to array

The code for this article is available on GitHub

The nested arrays consist of 2 elements - the key and the value.

You can iterate over the array of key-value pairs by using the forEach() method.

index.ts
const obj = { name: 'Bobby', country: 'Chile' }; // ๐Ÿ‘‡๏ธ const entries: [string, string][] const entries = Object.entries(obj); console.log(entries); // ๐Ÿ‘‰๏ธ [['name', 'Bobby'], ['country', 'Chile']] entries.forEach(([key, value]) => { // ๐Ÿ‘‡๏ธ name Bobby, country Chile console.log(key, value); });

Notice that we used array destructuring to destructure the key and value from the array.

Here is an example of how array destructuring works.

index.ts
const arr = ['bobby', 'hadz']; const [a, b] = arr; console.log(a); // ๐Ÿ‘‰๏ธ "bobby" console.log(b); // ๐Ÿ‘‰๏ธ "hadz"

We're basically unpacking the array's elements into variables. Note that the order is preserved.

I've also written an article on how to convert an object to a JSON string.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev