Skip to content
Related Articles

Related Articles

Improve Article

JavaScript | Object.fromEntries() Method

  • Last Updated : 27 Mar, 2020
Geek Week

The Object.fromEntries() method in JavaScript is standard built-in objects which is used to transforms 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 accept 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.

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



Example 1: Conversion of a Map into an Object.




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: Conversion of a Array into an Object.




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: Other Conversions




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 }

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

  • Google Chrome
  • Firefox
  • IE
  • Opera
  • Safari
  • Edge

Hey geek! The constant emerging technologies in the world of web development always keeps the excitement for this subject through the roof. But before you tackle the big projects, we suggest you start by learning the basics. Kickstart your web development journey by learning JS concepts with our JavaScript Course. Now at it’s lowest price ever!




My Personal Notes arrow_drop_up
Recommended Articles
Page :