| by Arround The Web | No comments

Convert Array to Object in JavaScript

While programming in JavaScript, you may need to enter multiple records, especially in the case of complex entries. For instance, when you want to access an item instantly or in the case of inserting and removing an element. In such cases, converting an array to an object in JavaScript is of great aid as they are comparatively much faster than the arrays and save a lot of time.

This blog will guide you related to transforming the specified array into an object in JavaScript.

How to Convert/Transform an Array to Object in JavaScript?

To convert/transform an array to object in JavaScript, the following approaches can be utilized:

Go through the mentioned methods one by one!

Method 1: Convert/Transform Array to Object in JavaScript Using Object.fromEntries() Method

The “Object.fromEntries()” method accepts a key-value pair as an argument and returns a new object. This method can be implemented to convert a two-dimensional array of integers and strings into objects.

Syntax

Object.fromEntries(arrtoObject)

In the given syntax, “arrtoObject” refers to the array which needs to be converted into an object.

Look at the below example for demonstration.

Example

In the following example, we will create a two-dimensional array named “arrtoObject” with the following key-value pairs:

const arrtoObject= [

[1, 'LinuxHint'],

[2, 'JavaScript'],

];

Now, apply the “Object.fromEntries()” method to convert the given array into object and display it:

const toObject= Object.fromEntries(arrtoObject);

console.log(toObject);

The corresponding output will be as follows:

Method 2: Convert/Transform an Array to Object in JavaScript Using Spread(…) Operator

The “Spread” operator (…) copies all or part of an existing array or object into another array or object. This method can be applied to target the array values and copy them into objects.

Syntax

[...arrtoObject];

In the above syntax, the spread operator “” will target all the array values stored in the particular “arrtoObject” variable.

The following example explains the stated concept.

Example

First, initialize an array with the specified string values:

const arrtoObject= ['These', 'are', 'objects'];

Now, apply the spread operator upon the declared array values using “”. This will result in converting the array values into objects and log it on the console:

const toObject= {...arrtoObject};

console.log("The converted array to object is:", toObject);

Output

Method 3: Convert/Transform an Array to Object in JavaScript Using Object.assign() Method

The “Object.assign()” method is implemented to place the values from one or more than one source object to a target object. This method can be utilized to convert the given string values in an array into target object values.

Syntax

Object.assign(target, ...sources)

Here, “target” refers to the target object, and “sources” are the properties that need to be applied.

Example

Firstly, store the following string values in an array named “arrtoObject”:

const arrtoObject= ['JavaScript', 'Objects'];

Next, apply the “Object.assign()” method to convert the given array passed in its parameter into the target object and display it on the console using the “console.log()” method:

const toObject= Object.assign({}, arrtoObject);

console.log("The converted array to object is:", toObject)

Output

Method 4: Convert/Transform Array to Object in JavaScript Using reduce() Method

The “reduce()” method implements a reducer function for array elements. This method can be applied to iterate along the specified array by passing its values to the object.

Look at the following example for demonstration.

Example

First, declare an array named “arrtoObject” with the following values:

const arrtoObject= ['Array', 'Object']

Next, apply the reduce() method with the help of a function to iterate along the created array. Here, “index” refers to the value’s index, and “key” is the corresponding value. Finally, log the target object values on the console:

console.log("The converted array to object is:", arrtoObject.reduce(function(target, key, index){

target[index] = key;

return target;}, {}))

Output

We have discussed various techniques to convert a given array into an object in JavaScript.

Conclusion

In JavaScript, you can utilize the “Object.fromEntries()” method to convert the two-dimensional array into an object, the “Spread(…)” operator method to target the given array values and copy them into objects, the “Object.assign()” method to convert the array into the target object in its parameter or the “reduce()” method to iterate along a particular array by passing it an object. This blog explained the methods for converting the specified array to an object in JavaScript.

Share Button

Source: linuxhint.com

Leave a Reply