{"id":3360,"date":"2024-11-10T08:35:51","date_gmt":"2024-11-10T00:35:51","guid":{"rendered":"https:\/\/fwq.ai\/blog\/3360\/"},"modified":"2024-11-10T08:35:51","modified_gmt":"2024-11-10T00:35:51","slug":"%e6%95%b0%e7%bb%84%e5%a6%82%e4%bd%95%e8%bd%ac%e5%8c%96%e4%b8%ba%e5%af%b9%e8%b1%a1js","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/3360\/","title":{"rendered":"\u6570\u7ec4\u5982\u4f55\u8f6c\u5316\u4e3a\u5bf9\u8c61js"},"content":{"rendered":"<blockquote><p>\n  \u4f7f\u7528 object.defineproperty()\u3001reduce()\u3001\u6269\u5c55\u8fd0\u7b97\u7b26\u3001map() \u548c fromentries() \u7b49\u65b9\u6cd5\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u6b65\u9aa4\u5c06\u6570\u7ec4\u8f6c\u6362\u4e3a\u5bf9\u8c61\uff1a\u4f7f\u7528 object.defineproperty() \u6307\u5b9a\u5c5e\u6027\u7279\u6027\u3002\u4f7f\u7528 reduce() \u7b80\u5316\u4ee3\u7801\u7f16\u5199\u3002\u4f7f\u7528\u6269\u5c55\u8fd0\u7b97\u7b26\u63d0\u9ad8\u5c0f\u6570\u7ec4\u5904\u7406\u6027\u80fd\u3002\u4f7f\u7528 map() \u548c fromentries() \u63d0\u9ad8\u5927\u6570\u7ec4\u5904\u7406\u6027\u80fd\uff0c\u4f46\u9700\u6ce8\u610f\u4f7f\u7528 map\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/08\/2024110810370686302.jpg\" class=\"aligncenter\" title=\"\u6570\u7ec4\u5982\u4f55\u8f6c\u5316\u4e3a\u5bf9\u8c61js\u63d2\u56fe\" alt=\"\u6570\u7ec4\u5982\u4f55\u8f6c\u5316\u4e3a\u5bf9\u8c61js\u63d2\u56fe\" \/><\/p>\n<p><strong>\u6570\u7ec4\u8f6c\u5bf9\u8c61<\/strong><\/p>\n<p>\u5728 JavaScript \u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\u5c06\u6570\u7ec4\u8f6c\u6362\u4e3a\u5bf9\u8c61\u3002<\/p>\n<p><strong>\u4f7f\u7528 Object.defineProperty()<\/strong><\/p>\n<pre>const array = [[\"name\", \"John\"], [\"age\", 25]];\nconst object = {};\n\narray.forEach((element) =&gt; {\n  Object.defineProperty(object, element[0], {\n    value: element[1],\n    enumerable: true,\n  });\n});<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>\u4f7f\u7528 reduce()<\/strong><\/p>\n<pre>const array = [[\"name\", \"John\"], [\"age\", 25]];\nconst object = array.reduce((acc, [key, value]) =&gt; {\n  acc[key] = value;\n  return acc;\n}, {});<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>\u4f7f\u7528 [ &#8230; ] \u6269\u5c55\u8fd0\u7b97\u7b26<\/strong><\/p>\n<pre>const array = [[\"name\", \"John\"], [\"age\", 25]];\nconst object = Object.assign({}, ...array);<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>\u4f7f\u7528 map()<\/strong><\/p>\n<pre>const array = [[\"name\", \"John\"], [\"age\", 25]];\nconst object = new Map(array);<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>\u4f7f\u7528 fromEntries()<\/strong><\/p>\n<pre>const array = [[\"name\", \"John\"], [\"age\", 25]];\nconst object = Object.fromEntries(array);<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>\u9009\u62e9\u65b9\u6cd5\u7684\u4f9d\u636e<\/strong><\/p>\n<p>\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\u53d6\u51b3\u4e8e\u6027\u80fd\u548c\u590d\u6742\u6027\u65b9\u9762\u7684\u8003\u8651\uff1a<\/p>\n<ul>\n<li>Object.defineProperty() \u5728\u5904\u7406\u5927\u6570\u7ec4\u65f6\u6027\u80fd\u8f83\u5dee\uff0c\u4f46\u53ef\u4ee5\u6307\u5b9a\u5c5e\u6027\u7684\u7279\u6027\u3002<\/li>\n<li>reduce() \u5bf9\u4e8e\u5904\u7406\u5927\u6570\u7ec4\u6765\u8bf4\u6027\u80fd\u8f83\u597d\uff0c\u4f46\u9700\u8981\u7f16\u5199\u66f4\u591a\u4ee3\u7801\u3002<\/li>\n<li>[ &#8230; ] \u6269\u5c55\u8fd0\u7b97\u7b26\u5728\u5904\u7406\u5c0f\u6570\u7ec4\u65f6\u6027\u80fd\u8f83\u597d\uff0c\u5e76\u4e14\u4ee3\u7801\u7b80\u6d01\u3002<\/li>\n<li>map() \u548c fromEntries() \u5bf9\u4e8e\u5904\u7406\u5927\u6570\u7ec4\u6765\u8bf4\u6027\u80fd\u8f83\u597d\uff0c\u4f46\u5b83\u4eec\u4f7f\u7528 Map \u800c\u4e0d\u662f\u666e\u901a\u5bf9\u8c61\u3002<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662f\u6570\u7ec4\u5982\u4f55\u8f6c\u5316\u4e3a\u5bf9\u8c61js\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4f7f\u7528 object.defineproperty()\u3001reduce()\u3001\u6269\u5c55\u8fd0\u7b97\u7b26\u3001map() \u548c fromentries() \u7b49\u65b9\u6cd5\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u6b65\u9aa4\u5c06\u6570\u7ec4\u8f6c\u6362\u4e3a\u5bf9\u8c61\uff1a\u4f7f\u7528 object.defineproperty() \u6307\u5b9a\u5c5e\u6027\u7279\u6027\u3002\u4f7f\u7528 reduce() \u7b80\u5316\u4ee3\u7801\u7f16\u5199\u3002\u4f7f\u7528\u6269\u5c55\u8fd0\u7b97\u7b26\u63d0\u9ad8\u5c0f\u6570\u7ec4\u5904\u7406\u6027\u80fd\u3002\u4f7f\u7528 map() \u548c fromentries() \u63d0\u9ad8\u5927\u6570\u7ec4\u5904\u7406\u6027\u80fd\uff0c\u4f46\u9700\u6ce8\u610f\u4f7f\u7528 map\u3002 \u6570\u7ec4\u8f6c\u5bf9\u8c61 \u5728 JavaScript \u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\u5c06\u6570\u7ec4\u8f6c\u6362\u4e3a\u5bf9\u8c61\u3002 \u4f7f\u7528 Object.defineProperty() const array = [[&#8220;name&#8221;, &#8220;John&#8221;], [&#8220;age&#8221;, 25]]; const object = {}; array.forEach((element) =&gt; { Object.defineProperty(object, element[0], { value: element[1], enumerable: true, }); }); \u767b\u5f55\u540e\u590d\u5236 \u4f7f\u7528 reduce() const array = [[&#8220;name&#8221;, &#8220;John&#8221;], [&#8220;age&#8221;, 25]]; const object [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-3360","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/3360","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=3360"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/3360\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=3360"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=3360"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=3360"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}