Convert an Array to an Object in JavaScript
Here is a need to convert an array into an object. To do so we are going to use a few of the most preferred methods. First here is a method to know.
Object.assign() method
This method copies the values of all enumerable own properties from source objects(one or more) to a target object.
Syntax:
Object.assign(target, ...sources)
Parameters:
- target: It specifies the target object.
- sources: It specifies the source object(s).
Example-1: This example converts the array to object by using Object.assign() method. For display it, JSON.stringify() method is used.
<!DOCTYPE html> <html> <head> <title> JavaScript | Convert Array to Object. </title> </head> <body style="text-align:center;" id="body"> <h1 style="color:green;"> GeeksForGeeks </h1> <p id="GFG_UP" style="font-size: 16px;"> </p> <button onclick="gfg_Run()"> Convert </button> <p id="GFG_DOWN" style="color:green; font-size: 20px; font-weight: bold;"> </p> <script> var el_up = document.getElementById("GFG_UP"); var el_down = document.getElementById("GFG_DOWN"); var array = [1, 2, 3, 4]; el_up.innerHTML = "Original Array = [" + array + "]";; function gfg_Run() { el_down.innerHTML = JSON.stringify(Object.assign({}, array)); } </script> </body> </html> |
Output:
-
Before clicking on the button:
-
After clicking on the button:
Example-2: This example converts the array to object by creating a function which adds the array values one by one to the object. For display it, JSON.stringify() method is used.
<!DOCTYPE html> <html> <head> <title> JavaScript | Convert Array to Object. </title> </head> <body style="text-align:center;" id="body"> <h1 style="color:green;"> GeeksForGeeks </h1> <p id="GFG_UP" style="font-size: 16px; font-weight: bold;"> </p> <button onclick="gfg_Run()"> Convert </button> <p id="GFG_DOWN" style="color:green; font-size: 22px; font-weight: bold;"> </p> <script> var el_up = document.getElementById("GFG_UP"); var el_down = document.getElementById("GFG_DOWN"); var array = ['GFG1', 'GFG2', 'GFG3', 'GFG4']; el_up.innerHTML = "Original Array = [" + array + "]"; function toObject(arr) { var rv = {}; for (var i = 0; i < arr.length; ++i) rv[i] = arr[i]; return rv; } function gfg_Run() { el_down.innerHTML = JSON.stringify(toObject(array)); } </script> </body> </html> |
Output:
-
Before clicking on the button:
-
After clicking on the button:
Recommended Posts:
- How to convert a plain object into ES6 Map using JavaScript ?
- Convert URL parameters to a JavaScript Object
- How to convert an object to string using JavaScript?
- How to Convert JS Object to JSON String in JQuery/Javascript?
- Convert an object to associative array in PHP
- How to convert an array into object using stdClass() in PHP?
- How to add an object to an array in JavaScript ?
- How to convert Set to Array in JavaScript?
- How to Convert Array to Set in JavaScript?
- How to check object is an array in JavaScript?
- How to push an array into the object in JavaScript ?
- How to convert Map keys to an array in JavaScript ?
- How to convert PHP array to JavaScript or JSON ?
- JavaScript | Convert an array to JSON
- Sort an Object Array by Date in JavaScript
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



