JavaScript .fromEntries()

jameskeezer's avatar
Published Dec 10, 2023
Contribute to Docs

The .fromEntries() method returns a new object with properties from a given list. A single element in a list can be an array with two elements. The first element in this array will be the key and the second element will be the value for a single property in the returned object.

  • A full-stack engineer can get a project done from start to finish, back-end to front-end.
    • Includes 51 Courses
    • With Professional Certification
    • Beginner Friendly.
      150 hours
  • Learn how to use JavaScript — a powerful and flexible programming language for adding website interactivity.
    • Beginner Friendly.
      15 hours

Syntax

Object.fromEntries(iterableObject);

The iterableObject is usually an Array or a Map.

Example

Creating an object from an array of key-value pairs.

const array = [
['0', 'Los Angeles'],
['1', 'Toronto'],
['2', 'Paris'],
];
const newObject = Object.fromEntries(array);
console.log(newObject);

The output would be:

{ '0': 'Los Angeles', '1': 'Toronto', '2': 'Paris' }

Codebyte Example

Following is a runnable example that demonstrates the .fromEntries() method.

Code
Output

All contributors

Contribute to Docs

Learn JavaScript on Codecademy

  • A full-stack engineer can get a project done from start to finish, back-end to front-end.
    • Includes 51 Courses
    • With Professional Certification
    • Beginner Friendly.
      150 hours
  • Learn how to use JavaScript — a powerful and flexible programming language for adding website interactivity.
    • Beginner Friendly.
      15 hours