# How to convert an array to an object in JavaScript

By Atta Ur Rehman Shah (https://attacomsian.com/about). Published Feb 24, 2020, updated Nov 6, 2022. Topics: JavaScript.
Canonical URL: https://attacomsian.com/blog/javascript-convert-array-to-object

> A detailed guide to learn about different ways to convert an array to an object in JavaScript.

There are four ways to convert an array to an object in JavaScript:

1. Using the `Object.assign()` method to convert an array to an object ,e.g. `const obj = Object.assign({}, arr)`.
2. Using the spread syntax (`...`) to convert an array to an object, e.g. `const obj = {...arr}`.
3. Using the `Array.forEach()` method to iterate over all items and add them as a key-value pair to an object.
4. Using the `Object.fromEntries()` method to convert an array of key-value pairs to an object, e.g. `const obj = Object.fromEntries(arr)`. 

## Convert an array to object using `Object.assign()` method

The `Object.assign()` method was introduced in ES6, and it copies the values of all enumerable own properties from one or more source objects to a target object. It has the following syntax:

```javascript
Object.assign(target, ...sources)
```

The `target` object is the first argument and is also used as the return value. The following example demonstrates how you can use the `Object.assign()` method to convert an [array](https://attacomsian.com/blog/javascript-arrays) to an  [object](https://attacomsian.com/blog/objects-prototypes-classes-javascript#objects):

```javascript
const names = ['Alex', 'Bob', 'Johny', 'Atta']

const obj = Object.assign({}, names)

console.log(obj)
// { '0': 'Alex', '1': 'Bob', '2': 'Johny', '3': 'Atta' }
```

Read [this guide](https://attacomsian.com/blog/javascript-object-assign) to learn more about the `Object.assign()` method.

## Convert an array to object using spread operator

Another way to convert an array to an object is by using the object spread (`{... iterable}`) operator. Here is an example:

```javascript
const names = ['Alex', 'Bob', 'Johny', 'Atta']

const obj = { ...names }

console.log(obj)
// { '0': 'Alex', '1': 'Bob', '2': 'Johny', '3': 'Atta' }
```

## Convert an array to object using `Array.forEach()` method

To use [`Array.forEach()`](https://attacomsian.com/blog/javascript-foreach) method to convert an array to an object:

1. Declare an empty object as a variable.
2. Use the `Array.forEach()` method to iterate over the array elements.
3. In each iteration, [add the element as a key-value pair](https://attacomsian.com/blog/javascript-object-add-key) to the object. 

```javascript
const names = ['Alex', 'Bob', 'Johny', 'Atta']

const obj = {}

names.forEach((elem, i) => {
  obj[i] = elem
})

console.log(obj)
// { 0: "Alex", 1: "Bob", 2: "Johny", 3: "Atta" }
```

Unlike the above methods, the `Array.forEach()` method allows you to name the keys of the new object.

```javascript
names.forEach((elem, i) => {
  obj[`key${i}`] = elem
})

console.log(obj)
// { key0: 'Alex', key1: 'Bob', key2: 'Johny', key3: 'Atta' }
```

You can also add a function to `Array`'s [prototype](https://attacomsian.com/blog/objects-prototypes-classes-javascript#prototypes) and call it whenever you want to convert an array into an object:

```javascript
Array.prototype.toObject = function () {
  const obj = {}

  this.forEach((elem, i) => {
    obj[i] = elem
  })

  return obj
}

const newObj = ['Alex', 'Bob', 'Johny', 'Atta'].toObject()

console.log(newObj)
// { '0': 'Alex', '1': 'Bob', '2': 'Johny', '3': 'Atta' }
```


## Convert an array to object using `Object.fromEntries()` method

The `Object.fromEntries()` method converts an array of key-value pairs into an object and returns the result, as shown below:

```javascript
const props = [
  ['name', 'John Doe'],
  ['age', 29]
]

const obj = Object.fromEntries(props)

console.log(obj)
// { name: 'John Doe', age: 29 }
```

Notice the nested arrays we used this time. These two-dimensional arrays contain at least two elements &mdash; a key and a value.
