JavaScript - Convert an Array to an Object
Last Updated :
21 Jun, 2025
Here are some common methods which can be used to convert an array to an object:
1. Using JavaScript Object.assign() method
The first approach is using the Object.assign() method. This method copies the values of all enumerable properties from source objects(one or more) to a target object.
JavaScript
let a = [1, 2, 3, 4];
let obj = Object.assign({}, a);
console.log(JSON.stringify(obj));
Output{"0":1,"1":2,"2":3,"3":4}
2. Using JavaScript for loop
In this method, we will traverse the array using a JavaScript for loop and assign each array element to the target object.
JavaScript
let a = ['GFG1', 'GFG2', 'GFG3', 'GFG4'];
let rv = {};
for (let i = 0; i < a.length; ++i)
rv[i] = a[i];
console.log(JSON.stringify(rv));
Output{"0":"GFG1","1":"GFG2","2":"GFG3","3":"GFG4"}
3. Using JavaScript Object.fromEntries() method
The Object.fromEntries() method in JavaScript is a standard built-in object which is used to transform a list of key-value pairs into an object. This method returns a new object whose properties are given by the entries of the iterable.
JavaScript
let a = [['JS', 'JavaScript'], ['GFG', 'GeeksforGeeks']];
let obj = Object.fromEntries(a);
console.log(obj);
Output{ JS: 'JavaScript', GFG: 'GeeksforGeeks' }
4. Using the Spread Operator
The Spread operator allows an iterable to expand in places where 0+ arguments are expected. It is mostly used in the variable array where there is more than 1 value is expected. It allows us the privilege to obtain a list of parameters from an array.
JavaScript
const a = ["Geeks", "for", "Geeks"];
const obj = {...a};
console.log(obj);
Output{ '0': 'Geeks', '1': 'for', '2': 'Geeks' }
5. Using forEach
Using the forEach method iterates over the array of key-value pairs. For each pair, the key is used as the object property, and the corresponding value is assigned. This approach dynamically builds an object from the array elements.
JavaScript
const a = ["Geeks", "for", "Geeks"];
const obj = {};
a.forEach((value, index) => obj[index] = value);
console.log(obj);
Output{ '0': 'Geeks', '1': 'for', '2': 'Geeks' }
6. Using JavaScript reduce Method
The reduce method is a powerful tool for transforming an array into a single value. You can use it to convert an array to an object by iterating over the array and building an object with key-value pairs.
JavaScript
const a = ['Hello', 'World', 'Welcome', 'To', 'JavaScript'];
const obj = a.reduce((acc, current, index) => {
acc[index] = current;
return acc;
}, {});
console.log(JSON.stringify(obj));
Output{"0":"Hello","1":"World","2":"Welcome","3":"To","4":"JavaScript"}
Conclusion
Converting an array to an object in JavaScript is a simple process, and there are multiple ways to do it. Depending on your needs, you can use methods like reduce(), Object.fromEntries(), or forEach() to convert an array into an object with the desired structure.
Similar Reads
JavaScript- Convert an Object to JS Array Objects in JavaScript are the most important data type and form the building blocks for modern JavaScript. These objects are quite different from JavaScriptâs primitive data types (Number, String, Boolean, null, undefined, and symbol). Methods to convert the Objects to JavaScript Array:1. Using Obje
3 min read
How to Convert Object to Array in JavaScript? In this article, we will learn how to convert an Object to an Array in JavaScript. Given an object, the task is to convert an object to an Array in JavaScript. Objects and Arrays are two fundamental data structures. Sometimes, it's necessary to convert an object to an array for various reasons, such
4 min read
JavaScript - Convert Two-Dimensional Array Into an Object Here are the different methods to convert the two-dimensional array into an object in JavaScript.1. Using a for LoopThe simplest way to convert a two-dimensional array into an object is by iterating through the array using a for loop and assigning key-value pairs.JavaScriptconst a = [['name', 'Abhay
2 min read
How to convert a map to array of objects in JavaScript? A map in JavaScript is a set of unique key and value pairs that can hold multiple values with only a single occurrence. Sometimes, you may be required to convert a map into an array of objects that contains the key-value pairs of the map as the values of the object keys. Let us discuss some methods
6 min read
How to Convert String to Array of Objects JavaScript ? Given a string, the task is to convert the given string to an array of objects using JavaScript. It is a common task, especially when working with JSON data received from a server or API. Below are the methods that allow us to convert string to an array of objects:Table of ContentUsing JSON.parse()
4 min read