Last modified: Jul 29, 2026

Convert Array to Object in JavaScript

Arrays and objects are two of the most common data structures in JavaScript. Sometimes you need to change an array into an object. This is a useful skill for organizing data.

An array stores items in an ordered list. An object stores key-value pairs. Converting an array to an object gives you faster lookups and better structure.

In this article, you will learn several ways to convert an array to an object. We will cover simple methods and more advanced techniques. Each method includes clear code examples.

This guide is perfect for beginners. You will see code comments to help you understand each step. Let's dive in.

Why Convert an Array to an Object?

Objects let you access data by a key name. This is faster than searching through an array. For example, if you have a list of users, an object lets you find a user by their ID instantly.

Objects also make your code more readable. Instead of remembering index numbers, you use meaningful keys. This reduces errors and improves maintainability.

Common use cases include: transforming API responses, building lookup tables, and grouping data. Converting arrays to objects is a fundamental skill in JavaScript.

Method 1: Using Object.assign()

The Object.assign() method copies all enumerable own properties from one or more source objects to a target object. You can use it to convert an array to an object.

When you pass an array to Object.assign(), it treats the array as an object with numeric keys (0, 1, 2, etc.). The result is an object with those numeric keys.

Here is an example:

// Create an array of fruits
const fruits = ['apple', 'banana', 'cherry'];

// Convert array to object using Object.assign
const fruitObject = Object.assign({}, fruits);

console.log(fruitObject);
// Output: { '0': 'apple', '1': 'banana', '2': 'cherry' }

The empty object {} is the target. The array is the source. The result is a new object with numeric keys.

This method is simple and fast. However, the keys are numbers, not descriptive names. If you need custom keys, use the next method.

Method 2: Using Spread Operator

The spread operator (...) expands an array into individual elements. You can use it inside an object literal to convert an array to an object.

This method works the same way as Object.assign(). It creates an object with numeric keys from the array indices.

Here is an example:

// Create an array of colors
const colors = ['red', 'green', 'blue'];

// Convert array to object using spread operator
const colorObject = { ...colors };

console.log(colorObject);
// Output: { '0': 'red', '1': 'green', '2': 'blue' }

The spread operator is concise and readable. It is a modern JavaScript feature. Use this method when you want a quick conversion with numeric keys.

Method 3: Using Array.reduce()

The Array.reduce() method is more flexible. It lets you create an object with custom keys. You can transform each array element into a key-value pair.

This method is ideal when you have an array of objects and want to use a property as the key. For example, converting an array of users to an object keyed by user ID.

Here is an example with an array of strings:

// Create an array of animals
const animals = ['cat', 'dog', 'fish'];

// Convert array to object with custom keys using reduce
const animalObject = animals.reduce((obj, animal, index) => {
  // Use the index as the key (or any custom key)
  obj[`animal_${index}`] = animal;
  return obj;
}, {});

console.log(animalObject);
// Output: { animal_0: 'cat', animal_1: 'dog', animal_2: 'fish' }

You can change the key name. For example, use the animal name as the key and the index as the value. The reduce method gives you full control.

Here is another example using an array of objects:

// Array of user objects
const users = [
  { id: 1, name: 'Alice' },
  { id: 2, name: 'Bob' },
  { id: 3, name: 'Charlie' }
];

// Convert to object keyed by user id
const userObject = users.reduce((obj, user) => {
  obj[user.id] = user;
  return obj;
}, {});

console.log(userObject);
// Output:
// {
//   '1': { id: 1, name: 'Alice' },
//   '2': { id: 2, name: 'Bob' },
//   '3': { id: 3, name: 'Charlie' }
// }

This is a powerful pattern. You can quickly look up a user by their ID. For more on working with arrays of objects, see our JavaScript Array of Objects Guide.

Method 4: Using Object.fromEntries()

The Object.fromEntries() method transforms a list of key-value pairs into an object. You need to first convert your array into an array of pairs.

This method is clean and readable. It works well when you have an array of pairs (two-element arrays).

Here is an example:

// Array of key-value pairs
const pairs = [
  ['name', 'John'],
  ['age', 30],
  ['city', 'New York']
];

// Convert to object using Object.fromEntries
const person = Object.fromEntries(pairs);

console.log(person);
// Output: { name: 'John', age: 30, city: 'New York' }

You can also use Array.map() to create the pairs from a simple array. This gives you custom keys.

// Simple array of values
const values = ['apple', 'banana', 'cherry'];

// Create pairs with custom keys
const fruitPairs = values.map((fruit, index) => [`fruit_${index}`, fruit]);

// Convert to object
const fruitObject = Object.fromEntries(fruitPairs);

console.log(fruitObject);
// Output: { fruit_0: 'apple', fruit_1: 'banana', fruit_2: 'cherry' }

The Object.fromEntries() method is the reverse of Object.entries(). It is a modern and efficient way to build objects from arrays.

Method 5: Using a for Loop

A traditional for loop is also an option. It gives you complete control over the conversion process. This method is good for beginners to understand the logic.

Here is an example:

// Create an array of numbers
const numbers = [10, 20, 30];

// Create an empty object
const numberObject = {};

// Loop through the array and add properties
for (let i = 0; i < numbers.length; i++) {
  numberObject[`key_${i}`] = numbers[i];
}

console.log(numberObject);
// Output: { key_0: 10, key_1: 20, key_2: 30 }

This loop method is easy to understand. You can modify the key names and values inside the loop. It is a good starting point for learning.

However, for modern code, using reduce or Object.fromEntries is more concise and less error-prone.

Practical Example: Converting an Array of Strings to an Object

Let's look at a real-world example. Suppose you have an array of country names. You want to create an object where each country is a key with a default value.

// Array of country names
const countries = ['USA', 'Canada', 'Mexico'];

// Convert to object with default value 'visited'
const countryObject = countries.reduce((obj, country) => {
  obj[country] = false;  // default value
  return obj;
}, {});

console.log(countryObject);
// Output: { USA: false, Canada: false, Mexico: false }

This object can be used as a checklist. You can later update the values to true when visited. This pattern is common in data processing.

For more on managing array lengths, check our JavaScript Array Length Guide.

Performance Considerations

When converting large arrays, performance matters. The Object.assign() and spread operator are fast for simple conversions. They are optimized in modern JavaScript engines.

The reduce method is slightly slower because it calls a function for each element. However, for most use cases, the difference is negligible.

If you need the fastest conversion for a large array, use a for loop. It avoids function call overhead. But always prioritize readability unless performance is critical.

Remember to test with your specific data size. For arrays under 10,000 items, any method works fine.

Common Mistakes to Avoid

One common mistake is forgetting to return the accumulator in reduce. This causes undefined errors. Always include the return statement.

Another mistake is using the spread operator on an array of objects. It creates an object with numeric keys, not the object properties. Use reduce for custom keys.

Also, be careful with duplicate keys. If your array has duplicate values, the last one overwrites the previous. This can lead to data loss.

Finally, remember that objects do not guarantee insertion order for numeric keys. For string keys, order is preserved in modern JavaScript.

Conclusion

Converting an array to an object in JavaScript is a common task with several solutions. You learned five methods: Object.assign(), spread operator, Array.reduce(), Object.fromEntries(), and a for loop.

Each method has its strengths. Use Object.assign() or spread for simple numeric keys. Use reduce for custom keys and complex transformations. Use Object.fromEntries() for key-value pairs.

Practice with your own data. Experiment with different methods. This will help you choose the right tool for each situation.

For more array techniques, see our JavaScript Array Methods Guide. Understanding arrays is a key part of becoming a skilled JavaScript developer.

Now you know how to convert an array to an object. Go ahead and apply these methods in your projects. Happy coding!