How to use Object.fromEntries in JavaScript?

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])

New on Educative
Learn to Code
Learn any Language as a beginner
Develop a human edge in an AI powered world and learn to code with AI from our beginner friendly catalog
🏆 Leaderboard
Daily Coding Challenge
Solve a new coding challenge every day and climb the leaderboard

Free Resources