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:
const array = ['a','b','c'] const obj = { ...array} console.log(obj) // Output: {0: 'a', 1: 'b', 2: 'c'}
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' }