Need Help With Javascript Development?

Work with our skilled Javascript developers to accelerate your project and boost its performance.

Hire JavaScript Developers

Support On Demand!

In JavaScript, you can convert an array into an object using various methods depending on how you want the conversion to occur. Here are a couple of common approaches:

Destructuring(Shorted way)

const array = ['a','b','c']
const obj = { ...array}
console.log(obj) // Output: {0: 'a', 1: 'b', 2: 'c'}

Loop Over Array

const array = ['a','b','c']
const obj = {};
array.forEach((value, index) => {
   obj[index] = value;
})
console.log(obj) // Output: {0: 'a', 1: 'b', 2: 'c'}

Using ES6 Object.fromEntries() Method
If your array contains pairs of key-value arrays (like [[‘key1’, ‘value1’], [‘key2’, ‘value2’]]), you can directly use Object.fromEntries() method

const array = [['key1', 'value1'], ['key2', 'value2'], ['key3', 'value3']];
const obj = Object.fromEntries(array);
console.log(obj); // Output: { key1: 'value1', key2: 'value2', key3: 'value3' }

Related Q&A