Skip to main content

How to Convert an Array to an Object in JavaScript

Converting an array into an object is a common data transformation task. However, the correct method depends entirely on the structure of your source array. Are you converting a simple array, an array of key-value pairs, or an array of objects?

This guide will teach you the modern, standard methods for each of these three common scenarios. You will learn how to use the spread syntax (...), Object.fromEntries(), and Array.prototype.reduce() to handle any array-to-object conversion cleanly and efficiently.

Scenario 1: Converting a Simple Array

In this scenario, you have a flat array of values, and you want to create an object where the array indexes become the object keys.

For example:

// Problem: Convert this array into an object like { 0: 'a', 1: 'b', 2: 'c' }.
const simpleArray = ['a', 'b', 'c'];

The recommended solution is to use the Spread Syntax (...). The spread syntax is the most concise and modern way to achieve this because it unpacks the array's elements into a new object literal.

const simpleArray = ['a', 'b', 'c'];

const myObject = { ...simpleArray };

console.log(myObject); // Output: { '0': 'a', '1': 'b', '2': 'c' }

An alternative is the Object.assign() method, which produces the same result:

const myObject = Object.assign({}, simpleArray);
console.log(myObject); // Output: { '0': 'a', '1': 'b', '2': 'c' }

Scenario 2: Converting an Array of Key-Value Pairs

This is a very common use case. You have a two-dimensional array where each inner array is a [key, value] pair, and you want to convert it into a single object.

For example, we want to convert this array of pairs into a single object.

const keyValueArray = [
['name', 'Alice'],
['age', 30],
];

The recommended solution is Object.fromEntries().

  • The Object.fromEntries() method was designed for this exact purpose.
  • It takes an iterable of key-value pairs and transforms it into an object.
const keyValueArray = [
['name', 'Alice'],
['age', 30],
];

const myObject = Object.fromEntries(keyValueArray);

console.log(myObject);

Output:

{ name: 'Alice', age: 30 }
note

This is the cleanest, most readable, and most efficient solution for this scenario.

Scenario 3: Converting an Array of Objects into a Single Object

In this scenario, you want to "index" an array of objects by a specific property (like an id), creating a single "lookup" object.

For example, we want to transform an array of user objects into a single object where each key is a user's id.

// Problem: Convert this array into an object keyed by the 'id' property.
const usersArray = [
{ id: 'user1', name: 'Alice' },
{ id: 'user2', name: 'Bob' },
];

And the desired output:

{
user1: { id: 'user1', name: 'Alice' },
user2: { id: 'user2', name: 'Bob' }
}

The recommended solution is Array.prototype.reduce(). The reduce() method is the perfect tool for transforming an array into a single value, in this case, an object.

const usersArray = [
{ id: 'user1', name: 'Alice' },
{ id: 'user2', name: 'Bob' },
];

const usersObject = usersArray.reduce((accumulator, currentObject) => {
accumulator[currentObject.id] = currentObject;
return accumulator;
}, {});

console.log(usersObject);

Output:

{
user1: { id: 'user1', name: 'Alice' },
user2: { id: 'user2', name: 'Bob' }
}

How it works:

  • reduce((acc, obj) => {...}, {}): We initialize the reduce function with an empty object ({}) as the initial value for the accumulator.
  • acc[obj.id] = obj;: On each iteration, we add a new property to our accumulator object. The key is the current object's id, and the value is the object itself.
  • return acc;: We return the updated accumulator for the next iteration.

Conclusion

The correct method for converting an array to an object depends entirely on the shape of your source array.

  • For a simple array where indexes become keys, use the spread syntax ({ ...myArray }).
  • For an array of [key, value] pairs, the definitive tool is Object.fromEntries(myArray).
  • For an array of objects that you want to index by a property, the most powerful and flexible method is myArray.reduce().

By identifying your specific scenario and choosing the appropriate modern tool, you can perform these transformations cleanly and efficiently.