Open In App

JavaScript Object fromEntries() Method

Improve
Improve
Like Article
Like
Save
Share
Report

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 )

Parameters: This method accepts a single parameter iterable which holds an iterable such as Array or Map or other objects implementing the iterable protocol. 

Return value: This method always returns a new object whose properties are given by the entries of the iterable. 

The below examples illustrate the Object.fromEntries() method in JavaScript: 

Example 1: In this example, we will convert a Map into an Object using the Object.fromEntries() method in JavaScript. 

javascript




const map1 = new Map([['big', 'small'], [1, 0]]);
const geek = Object.fromEntries(map1);
console.log(geek);
 
const map2 = new Map(
    [['Geek1', 'Intern'],
    ['stipend', 'Works basis']]
);
const geek1 = Object.fromEntries(map2);
console.log(geek1);


Output:

Object { 1: 0, big: "small" }
Object { Geek1: "Intern", stipend: "Works basis" }

Example 2: In this example, we will convert an Array into an Object using the Object.fromEntries() method in JavaScript. 

javascript




const arr1 = [['big', 'small'], [1, 0], ['a', 'z']];
const geek = Object.fromEntries(arr1);
console.log(geek);
 
const arr2 = [['Geek1', 'Intern'], ['stipend', 'Works basis']];
const geek1 = Object.fromEntries(arr2);
console.log(geek1);


Output:

Object { 1: 0, big: "small", a: "z" }
Object { Geek1: "Intern", stipend: "Works basis" }

Example 3: In this example, we will see some other Conversions to objects using Object.fromEntries() method in JavaScript.

javascript




const params = 'type=Get_the Value&geekno=34&paid=10';
const searchParams = new URLSearchParams(params);
 
console.log(Object.fromEntries(searchParams));
 
const object1 = { val1: 112, val2: 345, val3: 76 };
const object2 = Object.fromEntries(
    Object.entries(object1)
        .map(([key, val]) => [key, val * 3])
);
console.log(object2);


Output:

Object { type: "Get_the Value", geekno: "34", paid: "10" }
Object { val1: 336, val2: 1035, val3: 228 }

We have a complete list of Javascript Object methods, to check those please go through this JavaScript Object Complete Reference article.

Supported Browsers: The browsers supported by Object.fromEntries() method are listed below:

  • Google Chrome 73 and above
  • Firefox 63 and above
  • Opera 60 and above
  • Safari 12.1 and above
  • Edge 79 and above


Last Updated : 19 Jun, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads