diff options
author | Peter Bengtsson <mail@peterbe.com> | 2020-12-08 14:40:17 -0500 |
---|---|---|
committer | Peter Bengtsson <mail@peterbe.com> | 2020-12-08 14:40:17 -0500 |
commit | 33058f2b292b3a581333bdfb21b8f671898c5060 (patch) | |
tree | 51c3e392513ec574331b2d3f85c394445ea803c6 /files/zh-cn/web/javascript/reference/global_objects/object/fromentries | |
parent | 8b66d724f7caf0157093fb09cfec8fbd0c6ad50a (diff) | |
download | translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.gz translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.bz2 translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.zip |
initial commit
Diffstat (limited to 'files/zh-cn/web/javascript/reference/global_objects/object/fromentries')
-rw-r--r-- | files/zh-cn/web/javascript/reference/global_objects/object/fromentries/index.html | 105 |
1 files changed, 105 insertions, 0 deletions
diff --git a/files/zh-cn/web/javascript/reference/global_objects/object/fromentries/index.html b/files/zh-cn/web/javascript/reference/global_objects/object/fromentries/index.html new file mode 100644 index 0000000000..80cb1de95a --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/object/fromentries/index.html @@ -0,0 +1,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 ]) => [ 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> |