Convert an Array to an Object in JavaScript - GeeksforGeeks

14 May, 2019
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() methodThis 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:
javascript-array
javascript-object
JavaScript
Web Technologies
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Difference between var, let and const keywords in JavaScript
Difference Between PUT and PATCH Request
Remove elements from a JavaScript Array
How to get character array from string in JavaScript?
How to get selected value in dropdown list using JavaScript ?
Top 10 Front End Developer Skills That You Need in 2022
Installation of Node.js on Linux
Top 10 Projects For Beginners To Practice HTML and CSS Skills
How to fetch data from an API in ReactJS ?
How to insert spaces/tabs in text using HTML/CSS?