How to use Object.fromEntries() method In Javascript
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', 'w3wiki']];
// Create object
let obj = Object.fromEntries(arr);
// Display output
console.log(obj);
Output
{ JS: 'JavaScript', GFG: 'w3wiki' }
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:
Table of Content
- Using JavaScript Object.assign() method
- Using JavaScript for loop
- Using JavaScript Object.fromEntries() method
- Using the Spread Operator
- Using forEach