Table of contents
1.
Introduction
2.
1. Using Object.values() Method
2.1.
Syntax
2.2.
Example
3.
2. Using Object.keys() Method
3.1.
Syntax
3.2.
Example
4.
3. Using Object.entries() Method
4.1.
Syntax
4.2.
Example
5.
4. Using for...in Loop
5.1.
Syntax
5.2.
Example
6.
5. Using Object.getOwnPropertyNames() Method
6.1.
Syntax
6.2.
Example
7.
6. Using reduce()
7.1.
Syntax
7.2.
Example
8.
Frequently Asked Questions
8.1.
Which method should I use to get only the values of an object?
8.2.
How do I convert an object into an array of key-value pairs?
8.3.
Can I customize the conversion process?
9.
Conclusion
Last Updated: Jan 31, 2025
Easy

How to Convert an Object into Array of Objects in JavaScript?

Author Sinki Kumari
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Converting an object into an array of objects in JavaScript is a useful technique when handling structured data. This conversion makes it easier to process, filter, or display data in list-based formats, especially when working with arrays in JavaScript. 

JavaScript provides built-in methods like Object.entries(), Object.keys(), and Object.values() to transform objects into arrays. These methods allow developers to extract key-value pairs and format them into an array of objects, making data manipulation simpler and more efficient.

In this article, you will learn different ways to convert an object into array of objects in JavaScript, using step-by-step examples that are easy to understand and implement.

1. Using Object.values() Method

The Object.values() method retrieves all the values from an object and returns them in an array. This is helpful when you only need the values of the object.

Syntax

Object.values(object)

Example

const student = {
  name: "John",
  age: 20,
  course: "Computer Science"
};
const valuesArray = Object.values(student);
console.log(valuesArray);
You can also try this code with Online Javascript Compiler
Run Code


Output

["John", 20, "Computer Science"]


Explanation

  • Object.values(student) extracts all the values ("John", 20, and "Computer Science") and places them in an array.
     
  • Use this method when only the values are needed, not the keys.

2. Using Object.keys() Method

The Object.keys() method retrieves all the keys of an object and returns them in an array. This method is helpful when working with property names.

Syntax

Object.keys(object)

Example

const student = {
  name: "John",
  age: 20,
  course: "Computer Science"
};
const keysArray = Object.keys(student);
console.log(keysArray);
You can also try this code with Online Javascript Compiler
Run Code


Output

["name", "age", "course"]


Explanation

  • Object.keys(student) fetches the keys ("name", "age", and "course") from the object and stores them in an array.
     
  • This method is used when you want to loop through or manipulate keys.

3. Using Object.entries() Method

The Object.entries() method returns an array of key-value pairs. Each key-value pair is stored as an array inside a larger array.

Syntax

Object.entries(object)

Example

const student = {
  name: "John",
  age: 20,
  course: "Computer Science"
};
const entriesArray = Object.entries(student);
console.log(entriesArray);
You can also try this code with Online Javascript Compiler
Run Code


Output

[["name", "John"], ["age", 20], ["course", "Computer Science"]]


Explanation

  • Object.entries(student) creates an array of arrays, where each sub-array contains a key-value pair.
     
  • This method is useful when you need both keys and values for further processing.

4. Using for...in Loop

The for...in loop allows iteration over the keys of an object, enabling you to manually construct an array.

Syntax

for (key in object) {
  // code block
}

Example

const student = {
  name: "John",
  age: 20,
  course: "Computer Science"
};

const resultArray = [];
for (const key in student) {
  resultArray.push([key, student[key]]);
}
console.log(resultArray);
You can also try this code with Online Javascript Compiler
Run Code


Output

[["name", "John"], ["age", 20], ["course", "Computer Science"]]


Explanation

  • The for...in loop iterates over each key in the object.
     
  • The resultArray is manually populated with key-value pairs.
     
  • This method provides more control over the conversion process.

5. Using Object.getOwnPropertyNames() Method

The Object.getOwnPropertyNames() method returns an array of all property names (keys) of an object. It can be used to manually create an array of key-value pairs.

Syntax

Object.getOwnPropertyNames(object)

Example

const student = {
  name: "John",
  age: 20,
  course: "Computer Science"
};

const propertyNames = Object.getOwnPropertyNames(student);
const resultArray = propertyNames.map(key => [key, student[key]]);

console.log(resultArray);
You can also try this code with Online Javascript Compiler
Run Code


Output

[["name", "John"], ["age", 20], ["course", "Computer Science"]]


Explanation

  • Object.getOwnPropertyNames(student) fetches all the keys.
     
  • The map() function is then used to create an array of key-value pairs.
     
  • This method is beneficial for working with both keys and values.

6. Using reduce()

The reduce() method is a powerful array function that can transform an object into any desired format, including an array.

Syntax

array.reduce(callback, initialValue)

Example

const student = {
  name: "John",
  age: 20,
  course: "Computer Science"
};


const entriesArray = Object.keys(student).reduce((acc, key) => {
  acc.push([key, student[key]]);
  return acc;
}, []);

console.log(entriesArray);
You can also try this code with Online Javascript Compiler
Run Code


Output

[["name", "John"], ["age", 20], ["course", "Computer Science"]]


Explanation

  • Object.keys(student) fetches the keys.
     
  • reduce() iterates through the keys and builds an array of key-value pairs.
     
  • This method is useful when you need customized logic for conversion.

Frequently Asked Questions

Which method should I use to get only the values of an object?

Use the Object.values() method to extract all values from an object and store them in an array.

How do I convert an object into an array of key-value pairs?

You can use the Object.entries() method to create an array where each key-value pair is a sub-array.

Can I customize the conversion process?

Yes, using the reduce() method or a for...in loop allows you to customize how the object is converted into an array.

Conclusion

In this article, we learned about how to convert an object into array of objects in JavaScript using built-in methods like Object.entries(), Object.keys(), and Object.values(). These methods help in transforming object data into an array format, making it easier to process, iterate, and manipulate.

Each method has its unique use cases, and choosing the right one depends on your requirements.

Live masterclass