.fromEntries()

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.

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.

us
Visit us
code
Hide code
Code
Output
Hide output
Hide output
Loading...

All contributors

Looking to contribute?

Learn JavaScript on Codecademy