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.



filter_none

edit
close

play_arrow

link
brightness_4
code

<!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>

chevron_right


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.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!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>

chevron_right


Output:

  • Before clicking on the button:
  • After clicking on the button:


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.