JavaScript - How to Convert an Array to Object

Published on Aug 26, 2022

Hello dev's, Toady I'll show you how to convert an Array to an object. Sometimes we need into run where we need to convert to our array as an object. That's why my today's topic is about how we can easily convert our array into object in a pure javascript way.

Converting array to object

First we'll see the below code snippet

<html>
<head>
    <title>Convert an array into object in javascript - shouts.dev</title>
</head>
<body>
  
<script>
    var array = [1, 2, 3];
  
    var converted_object = Object.assign({},array);

    console.log(array);
    console.log(converted_object);
  
  
   
</script>
</body>
</html>

It'll produce the below output.

Array to Object Conversion

Here what we'll do is, first we'll take an array elements. And then we'll convert these array into object using Object.assign() method. For more info about Object.assign(), see here.

That's it for today. Hope you'll enjoy through this tutorial. If you've any question or face any difficulty, catch me in the comment section. Thanks for reading :)

Comments (0)