How to Convert Array to Object in JS?

Share Me
  •  
  •  
  •  
  • 1
  •  
  •  
  •  
  •  
  •  
  •  
  •  
    1
    Share

Hello Developers
Today I will show you a example of how to convert a array to an object in javascript section. I think you can understand this concept of how to convert array to json object in js. So this article i will give you a very simple example of javascript convert array to object. So you can do convert array into object in js.

Here ,i will use Object.assign() for converting array into json object using javascript. I will just pass object as argument in Object.assign(). I will give you a very simple example of how you can easily convert array into object in jquery in below.

Let’s see a simple example that will help you:

<!DOCTYPE html>
<html>
<head>
    <title>Javascript Array to Object Convert - itsolutionstuff.com</title>
</head>
<body>
  
<script>
      
    var myArray = [
        'one',
        'two',
        'three',
        'four',
        'five'
    ];
  
    var myObject = Object.assign({}, myArray);
  
    console.log(myObject);
  
</script>
  
</body>
</html>

Read Also : How to Print Iframe Content using Jquery?

Output:

{0: "one", 1: "two", 2: "three", 3: "four", 4: "five"}

  

0: "one"

1: "two"

2: "three"

3: "four"

4: "five"

__proto__: Object

I hope it can help you… Also you can follow us on Facebook.

About code chef

My name is Shahriar sagor. I'm a developer. I live in Bangladesh and I love to write tutorials and tips that will help to other Developer's. I am a big fan of PHP, Javascript, JQuery, Laravel, Codeigniter, VueJS, AngularJS and Bootstrap from the early stage.

View all posts by code chef →

Leave a Reply

Your email address will not be published. Required fields are marked *

ten − eight =