ES10 - Object fromEntries

This tutorial covers Object fromEntries method in ES2019 with examples..

ES10 Object fromEntries method

ES10 introduced fromEntries method in Object API. It is used to convert a list of key and values into an object. Object has existing entries method introduced in ES7,which converts object key and values into an array. Object.fromEntries is the reverse of entries API

Object.entries method Following example convert object to array

const myobject = { id: 42, name:"franc" };
const myarray = Object.entries(myobject); 
console.log(myarray); //[ [ 'id', 42 ], [ 'name', 'franc' ] ]

entries method returns array where elements contains key and values for each key and value pair of an object

Convert Array to Object using fromEntries method

As there is no method to transfer array returned from entries to an object. Es10 introduced this feature to transfer back to object

const myarray= [ [ 'id', 42 ], [ 'name', 'franc' ] ]
const object = Object.fromEntries(myarray); 
console.log(object); 

Convert Map to Object using fromEntries method

It is very easy to convert map to object using Object.fromEntries without this, We have to use for loop and create object using initialize the data

const mapData = new Map([
  ['1', 'franc'],
  ['2', 'john'],
  ['3', 'Andrew']
]);
const myobject = Object.fromEntries(mapData);
console.log(myobject); //{ 1: 'franc', 2: 'john', 3: 'Andrew' }

Here is an example
```javascript
let str1=" firstString";
let str2="secondString ";

str1.length // outputs 13
str1.trimStart(); // outputs firstString without space
str1.length // outputs 12

str2.length // outputs 13
str2.trimEnd(); // outputs secondString without space
str2.length // outputs 12