.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.

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
Loading...

All contributors

Contribute to Docs

Learn JavaScript on Codecademy