Movatterモバイル変換


[0]ホーム

URL:


  1. Web
  2. JavaScript
  3. Reference
  4. Standard built-in objects
  5. Object
  6. fromEntries()

Object.fromEntries()

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since ⁨January 2020⁩.

TheObject.fromEntries() static method transforms a list of key-value pairs into an object.

Try it

const entries = new Map([  ["foo", "bar"],  ["baz", 42],]);const obj = Object.fromEntries(entries);console.log(obj);// Expected output: Object { foo: "bar", baz: 42 }

Syntax

js
Object.fromEntries(iterable)

Parameters

iterable

Aniterable, such as anArray orMap, containing a list of objects. Each object should have two properties:

0

A string orsymbol representing the property key.

1

The property value.

Typically, this object is implemented as a two-element array, with the first element being the property key and the second element being the property value.

Return value

A new object whose properties are given by the entries of the iterable.

Description

TheObject.fromEntries() method takes a list of key-value pairs and returns a new object whose properties are given by those entries. Theiterable argument is expected to be an object that implements a[Symbol.iterator]() method. The method returns an iterator object that produces two-element array-like objects. The first element is a value that will be used as a property key, and the second element is the value to associate with that property key.

Object.fromEntries() performs the reverse ofObject.entries(), except thatObject.entries() only returns string-keyed properties, whileObject.fromEntries() can also create symbol-keyed properties.

Note:UnlikeArray.from(),Object.fromEntries() does not use the value ofthis, so calling it on another constructor does not create objects of that type.

Examples

Converting a Map to an Object

WithObject.fromEntries, you can convert fromMap toObject:

js
const map = new Map([  ["foo", "bar"],  ["baz", 42],]);const obj = Object.fromEntries(map);console.log(obj); // { foo: "bar", baz: 42 }

Converting an Array to an Object

WithObject.fromEntries, you can convert fromArray toObject:

js
const arr = [  ["0", "a"],  ["1", "b"],  ["2", "c"],];const obj = Object.fromEntries(arr);console.log(obj); // { 0: "a", 1: "b", 2: "c" }

Object transformations

WithObject.fromEntries, its reverse methodObject.entries(), andarray manipulation methods, you are able to transform objects like this:

js
const object1 = { a: 1, b: 2, c: 3 };const object2 = Object.fromEntries(  Object.entries(object1).map(([key, val]) => [key, val * 2]),);console.log(object2);// { a: 2, b: 4, c: 6 }

Specifications

Specification
ECMAScript® 2026 Language Specification
# sec-object.fromentries

Browser compatibility

See also

Help improve MDN

Learn how to contribute

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp