Open In App

Convert an Array to an Object in JavaScript

The object is defined as a key-value pair while the array is the collection of homogeneous data. we need to convert an array into an object. both are used to store the data in different formats. we will see the approach for conversion of an array into an object.

These are the following ways to solve this problem:

Method 1: Using JavaScript Object.assign() method

The first approach is using the Object.assign() method. This method copies the values of all enumerable properties from source objects(one or more) to a target object.

Syntax:

Object.assign(target, ...sources);

Example: This example converts the array to an object by using Object.assign() method. To display it, JSON.stringify() method is used. 

// Input array
let arr = [1, 2, 3, 4];

// Using Object.assign to create
// new object from array
let obj = Object.assign({}, arr);

// Display output
console.log(JSON.stringify(obj));

Output
{"0":1,"1":2,"2":3,"3":4}

Method 2: Using JavaScript for loop

In this method, we will traverse the array using a JavaScript for loop and assign each array element to the target object.

Example: In this example, the code converts the array to an object by creating a function that adds the array values one by one to the object. To display the object content, JSON.stringify() method is used.

// Input array
let arr = ['GFG1', 'GFG2', 'GFG3', 'GFG4'];
    
// Function to create object
function toObject(arr) {
    // target object
    let rv = {};
    // Traverse array using loop
    for (let i = 0; i < arr.length; ++i)
        // Assign each element to object
        rv[i] = arr[i];
    return rv;
}
// Display output
console.log(JSON.stringify(toObject(arr)));

Output
{"0":"GFG1","1":"GFG2","2":"GFG3","3":"GFG4"}

Method 3: Using JavaScript Object.fromEntries() method

The Object.fromEntries() method in JavaScript is a standard built-in object which is used to transform a list of key-value pairs into an object. This method returns a new object whose properties are given by the entries of the iterable.

Syntax:

Object.fromEntries( iterable );

Example: In this example, we have used Object.from.entries() method to convert an Array to an Object in JavaScript

// Input array
let arr = [['JS', 'JavaScript'], ['GFG', 'GeeksforGeeks']];
// Create object
let obj = Object.fromEntries(arr);

// Display output
console.log(obj);

Output
{ JS: 'JavaScript', GFG: 'GeeksforGeeks' }

Method 4: Using the Spread Operator

The Spread operator allows an iterable to expand in places where 0+ arguments are expected. It is mostly used in the variable array where there is more than 1 value is expected. It allows us the privilege to obtain a list of parameters from an array. 

Syntax:

let variablename1 = [...value]; 

Example: In this example, we have used spread operator to convert array to object.

const arr = ["Geeks", "for", "Geeks"];
const obj = {...arr};
console.log(obj);

Output
{ '0': 'Geeks', '1': 'for', '2': 'Geeks' }

Method 5: Using forEach

Using the forEach method iterates over the array of key-value pairs. For each pair, the key is used as the object property, and the corresponding value is assigned. This approach dynamically builds an object from the array elements.

Example: In this example we iterates over each element of the array arr using forEach and assigns each element as a property of the obj object with its index as the key.

const arr = ["Geeks", "for", "Geeks"];
const obj = {};
arr.forEach((value, index) => obj[index] = value);
console.log(obj);

Output
{ '0': 'Geeks', '1': 'for', '2': 'Geeks' }


Article Tags :