0
0
JavascriptHow-ToBeginner · 2 min read

JavaScript How to Convert Array to Object Easily

You can convert an array to an object in JavaScript using Object.fromEntries(array) if the array contains key-value pairs, or by using array.reduce((obj, item) => { obj[key] = value; return obj; }, {}) to build an object manually.
📋

Examples

Input[['a', 1], ['b', 2]]
Output{"a":1,"b":2}
Input[{id: 'x', value: 10}, {id: 'y', value: 20}]
Output{"x":10,"y":20}
Input[]
Output{}
🧠

How to Think About It

To convert an array to an object, first decide what should be the keys and values in the object. If the array already has pairs like [key, value], you can directly convert it. Otherwise, you can loop through the array and assign each element's property as a key and its value as the object value.
📐

Algorithm

1
Get the input array.
2
Check if the array contains key-value pairs or objects with key properties.
3
If key-value pairs, use a built-in method to convert directly.
4
If objects, loop through each element and assign the desired key and value to a new object.
5
Return the constructed object.
💻

Code

javascript
const arr = [['a', 1], ['b', 2]];
const obj = Object.fromEntries(arr);
console.log(obj);

// For array of objects
const arr2 = [{id: 'x', value: 10}, {id: 'y', value: 20}];
const obj2 = arr2.reduce((acc, item) => {
  acc[item.id] = item.value;
  return acc;
}, {});
console.log(obj2);
Output
{"a":1,"b":2} {"x":10,"y":20}
🔍

Dry Run

Let's trace converting [['a', 1], ['b', 2]] to an object using Object.fromEntries.

1

Start with array

[['a', 1], ['b', 2]]

2

Convert using Object.fromEntries

Object.fromEntries([['a', 1], ['b', 2]])

3

Resulting object

{"a":1,"b":2}

KeyValue
a1
b2
💡

Why This Works

Step 1: Using Object.fromEntries

This method takes an array of [key, value] pairs and creates an object where each key maps to its value.

Step 2: Using reduce for custom arrays

When the array has objects, reduce lets you build an object by assigning keys and values from each element.

Step 3: Returning the object

After processing all elements, the final object contains all keys and values from the array.

🔄

Alternative Approaches

Using forEach loop
javascript
const arr = [{id: 'x', value: 10}, {id: 'y', value: 20}];
const obj = {};
arr.forEach(item => {
  obj[item.id] = item.value;
});
console.log(obj);
Simple and readable but less functional style than reduce.
Using Map and Object.fromEntries
javascript
const arr = [{id: 'x', value: 10}, {id: 'y', value: 20}];
const obj = Object.fromEntries(arr.map(item => [item.id, item.value]));
console.log(obj);
Combines map and fromEntries for concise code.

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

Time Complexity

The conversion loops through each element once, so time grows linearly with array size.

Space Complexity

A new object is created with one property per array element, so space grows linearly.

Which Approach is Fastest?

Using Object.fromEntries is fastest for arrays of pairs; reduce and forEach have similar performance for arrays of objects.

ApproachTimeSpaceBest For
Object.fromEntriesO(n)O(n)Arrays of [key, value] pairs
reduceO(n)O(n)Arrays of objects with keys and values
forEach loopO(n)O(n)Simple, readable conversion for arrays of objects
💡
Use Object.fromEntries for arrays of pairs and reduce for arrays of objects to convert arrays to objects easily.
⚠️
Trying to convert arrays without key-value pairs directly to objects without specifying keys causes unexpected results.