Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

javascript

How to use Object.fromEntries in JavaScript?

Programming Bytes

Object.fromEntries

This method is used to convert an array of key-value pairs into an object.

  • The fromEntries method takes an argumentwhich is an iterable object such as an array, Map, or object that implements the iterable protocol.
  • The fromEntries method returns a new object created from the iterable object passed as an argument.

Creating an Object from an array

Console

Creating an Object from Map

Console

Tip: Shallow clone an Object

We can use fromEntries to clone an object. To do this, you need to convert the object to entries and create the object using fromEntries.

Console

Make sure that only the key-value pair is passed; otherwise, there will be an error.

The below code will throw an error

// TypeError will be thrown in all the below case 
Object.fromEntries(undefined)
Object.fromEntries(null)
Object.fromEntries(true)
Object.fromEntries(100)
Object.fromEntries("hi")
Object.fromEntries({key: "value"})
Object.fromEntries([1,2,3])

RELATED TAGS

javascript
RELATED COURSES

View all Courses

Keep Exploring