JavaScript Convert an Array to Object
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.
Here what we'll do is, first we'll take an array of elements. And then we'll convert this array into an object using Object.assign() method. For more info about Object.assign(), see here.
That's it for today. I hope you've enjoyed this tutorial. If you've any questions or face any difficulty, catch me in the comment section. Thanks for reading. :)