aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/javascript/reference/global_objects/object/fromentries/index.html
blob: 80cb1de95a2e0ab3035766a38c8b040a72fde220 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
---
title: Object.fromEntries()
slug: Web/JavaScript/Reference/Global_Objects/Object/fromEntries
translation_of: Web/JavaScript/Reference/Global_Objects/Object/fromEntries
---
<div>{{JSRef}}</div>

<p> <code><strong>Object.fromEntries()</strong></code> 方法把键值对列表转换为一个对象。</p>

<div>{{EmbedInteractiveExample("pages/js/object-fromentries.html")}}</div>



<h2 id="语法">语法</h2>

<pre class="syntaxbox notranslate">Object.fromEntries(<var>iterable</var>);</pre>

<h3 id="参数">参数</h3>

<dl>
 <dt><code>iterable</code></dt>
 <dd>类似 {{jsxref("Array")}} 、 {{jsxref("Map")}} 或者其它实现了<a href="/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#The_iterable_protocol">可迭代协议</a>的可迭代对象。</dd>
</dl>

<h3 id="返回值">返回值</h3>

<p>一个由该迭代对象条目提供对应属性的新对象。</p>

<h2 id="描述">描述</h2>

<p><code>Object.fromEntries()</code> 方法接收一个键值对的列表参数,并返回一个带有这些键值对的新对象。这个迭代参数应该是一个能够实现<code>@@iterator</code>方法的的对象,返回一个迭代器对象。它生成一个具有两个元素的类数组的对象,第一个元素是将用作属性键的值,第二个元素是与该属性键关联的值。</p>

<p><code>Object.fromEntries()</code> 执行与 {{jsxref("Object.entries")}} 互逆的操作。</p>

<h2 id="示例">示例</h2>

<h3 id="Map_转化为_Object"> <code>Map</code> 转化为 <code>Object</code></h3>

<p>通过 <code>Object.fromEntries</code>, 可以将 {{jsxref("Map")}} 转换为 {{jsxref("Object")}}:</p>

<pre class="brush: js notranslate">const map = new Map([ ['foo', 'bar'], ['baz', 42] ]);
const obj = Object.fromEntries(map);
console.log(obj); // { foo: "bar", baz: 42 }
</pre>

<h3 id="Array_转化为_Object"><code>Array</code> 转化为 <code>Object</code></h3>

<p>通过 <code>Object.fromEntries</code>, 可以将 {{jsxref("Array")}} 转换为 {{jsxref("Object")}}:</p>

<pre class="brush: js notranslate">const arr = [ ['0', 'a'], ['1', 'b'], ['2', 'c'] ];
const obj = Object.fromEntries(arr);
console.log(obj); // { 0: "a", 1: "b", 2: "c" }
</pre>

<h3 id="对象转换">对象转换</h3>

<p><code>Object.fromEntries</code> 是与 {{jsxref("Object.entries()")}} 相反的方法,用 <a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array#Methods_2">数组处理函数</a> 可以像下面这样转换对象:</p>

<pre class="brush: js notranslate">const object1 = { a: 1, b: 2, c: 3 };

const object2 = Object.fromEntries(
  Object.entries(object1)
  .map(([ key, val ]) =&gt; [ key, val * 2 ])
);

console.log(object2);
// { a: 2, b: 4, c: 6 }</pre>

<h2 id="规范">规范</h2>

<table class="standard-table">
 <thead>
  <tr>
   <th scope="col">规范</th>
   <th scope="col">状态</th>
   <th scope="col">备注</th>
  </tr>
 </thead>
 <tbody>
  <tr>
   <td>{{SpecName('ESDraft', '#sec-object.fromentries', 'Object.fromEntries')}}</td>
   <td>{{Spec2('ESDraft')}}</td>
   <td>在 ECMAScript 2019 中首次被定义。</td>
  </tr>
 </tbody>
</table>

<h2 id="浏览器兼容">浏览器兼容</h2>

<div>


<p>{{Compat("javascript.builtins.Object.fromEntries")}}</p>
</div>

<h2 id="相关链接">相关链接</h2>

<ul>
 <li>{{jsxref("Object.entries()")}}</li>
 <li>{{jsxref("Object.keys()")}}</li>
 <li>{{jsxref("Object.values()")}}</li>
 <li>{{jsxref("Map.prototype.entries()")}}</li>
 <li>{{jsxref("Map.prototype.keys()")}}</li>
 <li>{{jsxref("Map.prototype.values()")}}</li>
</ul>