How to Convert Array Into Object In Jquery?

Jquery

Nicesnippets

367

16-06-2021


Hi Guys,

In this example,I will learn you how to convert array into object in jquery.you can easy and simply convert array into object in jquery.

We will use Object.assign() for converting array into json object using jquery. we have to just pass object as argument in Object.assign().let's bellow example.

Example :


<!DOCTYPE html>

<html>

<head>

<title>Jquery Array to Object Convert - Nicesnippets.com</title>

</head>

<body>

<script>

var myArray = [

'apple',

'banana',

'orange',

'mango',

'pineapple'

];

var myObject = Object.assign({}, myArray);

console.log(myObject);

</script>

</body>

</html>

Output:

{0: "apple", 1: "banana", 2: "orange", 3: "mango", 4: "pineapple"}

0: "apple"

1: "banana"

2: "orange"

3: "mango"

4: "pineapple"

It will help you...


Recommended Posts