Bacancy Technology
Bacancy Technology represents the connected world, offering innovative and customer-centric information technology experiences, enabling Enterprises, Associates and the Society to Rise™.
12+
Countries where we have happy customers
1050+
Agile enabled employees
06
World wide offices
12+
Years of Experience
05
Agile Coaches
14
Certified Scrum Masters
1000+
Clients projects
1458
Happy customers
Artificial Intelligence
Machine Learning
Salesforce
Microsoft
SAP
August 27, 2024
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' }