Convert an Object to an Array of Objects in JavaScript

avatar
Borislav Hadzhiev

Last updated: Mar 4, 2024
3 min

banner

# Table of Contents

  1. Convert an Object to an Array of Objects in JavaScript
  2. Convert an Object to an Array of Objects using Object.keys()
  3. Convert an Object to an Array of Objects using Object.entries()
  4. Convert an Object to an Array preserving the nested keys

# Convert an Object to an Array of Objects in JavaScript

Use the Object.values() method to convert an object to an array of objects, e.g. const arr = Object.values(obj).

The Object.values() method takes an object as a parameter and returns an array containing the object's property values.

index.js
const obj = { emp1: { id: 1, name: 'Alice', }, emp2: { id: 2, name: 'Bob', }, }; const arrayOfObjects = Object.values(obj); // ๐Ÿ‘‡๏ธ [ { id: 1, name: 'Alice' }, { id: 2, name: 'Bob' } ] console.log(arrayOfObjects);

convert object to array of objects

The code for this article is available on GitHub

The Object.values() method returns an array containing the object's values.

Notice that the keys of the object get automatically removed. If you need to preserve the keys, look at the last subheading.

Here's an example of using the Object.values() method without nested properties.

index.js
// ๐Ÿ‘‡๏ธ ['bobby', 'Chile'] console.log(Object.values({name: 'bobby', country: 'Chile'}));

# Convert an Object to an Array of Objects using Object.keys()

You can also use the Object.keys method to convert an object to an array of objects.

index.js
const obj = { emp1: { id: 1, name: 'Alice', }, emp2: { id: 2, name: 'Bob', }, }; const arrayOfObjects = Object.keys(obj).map(key => obj[key]); // ๐Ÿ‘‡๏ธ [{id: 1, name: 'Alice'}, {id: 2, name: 'Bob'}] console.log(arrayOfObjects);

convert object to array of objects using object keys

The code for this article is available on GitHub

The Object.keys() method returns an array of the object's keys.

index.js
const obj = { emp1: { id: 1, name: 'Alice', }, emp2: { id: 2, name: 'Bob', }, }; console.log(Object.keys(obj)); // ๐Ÿ‘‰๏ธ [ 'emp1', 'emp2' ]

The function we passed to the Array.map() method gets called with each element in the array.

On each iteration, we return the value of each key.

The map() method returns a new array containing the values returned from the callback function.

# Convert an Object to an Array of Objects using Object.entries()

The Object.entries() method can be used to transform the object into an array of key-value pairs.

index.js
const obj = { emp1: { id: 1, name: 'Alice', }, emp2: { id: 2, name: 'Bob', }, }; // [ // [ 'emp1', { id: 1, name: 'Alice' } ], // [ 'emp2', { id: 2, name: 'Bob' } ] // ] console.log(Object.entries(obj));

convert object to array of objects using object entries

The code for this article is available on GitHub

You can use the Array.map() method to return only the value of each key.

index.js
const obj = { emp1: { id: 1, name: 'Alice', }, emp2: { id: 2, name: 'Bob', }, }; const arrayOfObjects = Object.entries(obj).map(entry => entry[1]); // ๐Ÿ‘‡๏ธ [{id: 1, name: 'Alice'}, {id: 2, name: 'Bob'}] console.log(arrayOfObjects);

# Convert an Object to an Array preserving the nested keys

You can also use the Object.entries() method to preserve the nested keys of the object when converting it to an array of objects.

index.js
const obj = { emp1: { id: 1, name: 'Alice', }, emp2: { id: 2, name: 'Bob', }, }; const withNestedKeys = Object.entries(obj).map(entry => { return {[entry[0]]: entry[1]}; }); // [ // { emp1: { id: 1, name: 'Alice' } }, // { emp2: { id: 2, name: 'Bob' } } // ] console.log(withNestedKeys);

convert object to array preserving nested keys

The code for this article is available on GitHub

We used the Array.map() method to iterate over the array.

On each iteration, we return an object and preserve the key-value pair structure of the original object.

When you use this approach all of the key-value pairs of the object get transferred to the objects in the array.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

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