Learn html, css, javascript online and how to make a webpage design

How to Convert Array to Object in Javascript?

Tuesday 5th of January 2021 10:07:46 PMArrayObjectJavascript

When working with Javascript sometimes we will need to convert the data type of an Array into an Abject.

Today we will learn how to convert an array into an object. We'll use Object.asign to do this.

Check out the example below:

<!DOCTYPE html>
<html>
<head>
    <title>How to convert Array to Object in Javascript - HtmlcssDownload.com</title>
</head>
<body>
  
<script>
      
    var myArray = [
        'HTML',
        'CSS',
        'Javascript',
        'PHP',
        'Java',
        'Python'
    ];
  
    var myObject = Object.assign({}, myArray);
  
    console.log(myObject);
  
</script>
  
</body>
</html>

 

Output:

Object
   0: "HTML"
   1: "CSS"
   2: "Javascript"
   3: "PHP"
   4: "Java"
   5: "Python"
   __proto__: Object

 

We are Recommending you: