0
0
JavascriptHow-ToBeginner · 2 min read

JavaScript How to Convert Object to Array Easily

Use Object.keys(obj) to get an array of keys, Object.values(obj) for values, or Object.entries(obj) for key-value pairs as arrays.
📋

Examples

Input{"a":1,"b":2}
Output["a","b"] (keys), [1,2] (values), [["a",1],["b",2]] (entries)
Input{"name":"John","age":30}
Output["name","age"], ["John",30], [["name","John"],["age",30]]
Input{}
Output[], [], []
🧠

How to Think About It

To convert an object to an array, decide if you want just the keys, just the values, or both as pairs. JavaScript provides built-in methods to extract these parts as arrays easily.
📐

Algorithm

1
Get the input object.
2
Choose what to extract: keys, values, or entries.
3
Use Object.keys() for keys array.
4
Use Object.values() for values array.
5
Use Object.entries() for array of [key, value] pairs.
6
Return the resulting array.
💻

Code

javascript
const obj = {a: 1, b: 2};

const keysArray = Object.keys(obj);
const valuesArray = Object.values(obj);
const entriesArray = Object.entries(obj);

console.log('Keys:', keysArray);
console.log('Values:', valuesArray);
console.log('Entries:', entriesArray);
Output
Keys: [ 'a', 'b' ] Values: [ 1, 2 ] Entries: [ [ 'a', 1 ], [ 'b', 2 ] ]
🔍

Dry Run

Let's trace converting {a:1, b:2} to arrays using Object.keys(), Object.values(), and Object.entries().

1

Start with object

obj = {a:1, b:2}

2

Get keys array

Object.keys(obj) returns ['a', 'b']

3

Get values array

Object.values(obj) returns [1, 2]

4

Get entries array

Object.entries(obj) returns [['a', 1], ['b', 2]]

MethodResult
Object.keys(obj)['a', 'b']
Object.values(obj)[1, 2]
Object.entries(obj)[['a', 1], ['b', 2]]
💡

Why This Works

Step 1: Extract keys

Object.keys(obj) returns an array of all property names (keys) in the object.

Step 2: Extract values

Object.values(obj) returns an array of all property values in the same order as keys.

Step 3: Extract entries

Object.entries(obj) returns an array of arrays, each containing a key and its corresponding value.

🔄

Alternative Approaches

Using for...in loop
javascript
const obj = {a:1, b:2};
const arr = [];
for (const key in obj) {
  if (obj.hasOwnProperty(key)) {
    arr.push([key, obj[key]]);
  }
}
console.log(arr);
Manual method to create entries array; less concise but works in older environments.
Using Object.keys() with map
javascript
const obj = {a:1, b:2};
const entries = Object.keys(obj).map(key => [key, obj[key]]);
console.log(entries);
Transforms keys into entries array; useful if you want to process keys before pairing.

Complexity: O(n) time, O(n) space

Time Complexity

All methods iterate over each property once, so time grows linearly with the number of properties.

Space Complexity

New arrays are created to hold keys, values, or entries, so space also grows linearly.

Which Approach is Fastest?

Built-in methods like Object.keys(), Object.values(), and Object.entries() are optimized and usually faster than manual loops.

ApproachTimeSpaceBest For
Object.keys()O(n)O(n)Getting keys array quickly
Object.values()O(n)O(n)Getting values array quickly
Object.entries()O(n)O(n)Getting key-value pairs as arrays
for...in loopO(n)O(n)Manual control, older environments
Object.keys() + mapO(n)O(n)Custom processing of keys before pairing
💡
Use Object.entries() when you need both keys and values as pairs in an array.
⚠️
Trying to convert an object directly with Array.from() without extracting keys or values first.