{"id":3418,"date":"2024-11-10T08:44:04","date_gmt":"2024-11-10T00:44:04","guid":{"rendered":"https:\/\/fwq.ai\/blog\/3418\/"},"modified":"2024-11-10T08:44:04","modified_gmt":"2024-11-10T00:44:04","slug":"node-js%e5%a6%82%e4%bd%95%e5%be%aa%e7%8e%afjs","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/3418\/","title":{"rendered":"node.js+\u5982\u4f55\u5faa\u73afjs"},"content":{"rendered":"<blockquote><p>\n  node.js \u5faa\u73af javascript \u5bf9\u8c61\u7684\u65b9\u6cd5\u5305\u62ec\uff1afor&#8230;in \u5faa\u73af\uff1a\u904d\u5386\u5bf9\u8c61\u7684\u952e\u548c\u503c\u3002for&#8230;of \u5faa\u73af\uff1a\u904d\u5386\u5bf9\u8c61\u7684\u5b9e\u9645\u503c\u3002object.keys() \u548c object.values()\uff1a\u5206\u522b\u904d\u5386\u5bf9\u8c61\u7684\u952e\u548c\u503c\u3002map \u5bf9\u8c61\uff1a\u5c06\u5bf9\u8c61\u8f6c\u6362\u4e3a map\uff0c\u5141\u8bb8\u904d\u5386\u952e\u503c\u5bf9\u3002\u5e93\u51fd\u6570\uff1a\u4f8b\u5982 lodash \u63d0\u4f9b\u66f4\u9ad8\u7ea7\u7684\u5faa\u73af\u65b9\u6cd5\uff0c\u5982 _.each()\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/08\/2024110811574983399.jpg\" class=\"aligncenter\" title=\"node.js+\u5982\u4f55\u5faa\u73afjs\u63d2\u56fe\" alt=\"node.js+\u5982\u4f55\u5faa\u73afjs\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u5728 Node.js \u4e2d\u5faa\u73af JavaScript \u5bf9\u8c61<\/strong><\/p>\n<p>Node.js \u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u6cd5\u6765\u5faa\u73af JavaScript \u5bf9\u8c61\uff0c\u5305\u62ec\uff1a<\/p>\n<p><strong>1. for&#8230;in \u5faa\u73af\uff1a<\/strong><\/p>\n<pre>const object = {name: 'John Doe', age: 30};\n\nfor (const key in object) {\n  console.log(`Key: ${key}, Value: ${object[key]}`);\n}<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>2. for&#8230;of \u5faa\u73af\uff1a<\/strong><\/p>\n<pre>const object = {name: 'John Doe', age: 30};\n\nfor (const value of Object.values(object)) {\n  console.log(`Value: ${value}`);\n}<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>3. Object.keys() \u548c Object.values()\uff1a<\/strong><\/p>\n<pre>const object = {name: 'John Doe', age: 30};\n\nObject.keys(object).forEach(key =&gt; console.log(`Key: ${key}`));\n\nObject.values(object).forEach(value =&gt; console.log(`Value: ${value}`));<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>4. Map \u5bf9\u8c61\uff1a<\/strong><\/p>\n<pre>const object = {name: 'John Doe', age: 30};\nconst map = new Map(Object.entries(object));\n\nfor (const [key, value] of map) {\n  console.log(`Key: ${key}, Value: ${value}`);\n}<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>5. \u5e93\u51fd\u6570\uff1a<\/strong><\/p>\n<p>\u7b2c\u4e09\u65b9\u5e93\u8fd8\u63d0\u4f9b\u4e86\u66f4\u9ad8\u7ea7\u7684\u5faa\u73af\u65b9\u6cd5\uff0c\u4f8b\u5982 lodash \u4e2d\u7684 _.each()\u3002<\/p>\n<pre>const _ = require('lodash');\n\nconst object = {name: 'John Doe', age: 30};\n\n_.each(object, (value, key) =&gt; {\n  console.log(`Key: ${key}, Value: ${value}`);\n});<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fnode.js+\u5982\u4f55\u5faa\u73afjs\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>node.js \u5faa\u73af javascript \u5bf9\u8c61\u7684\u65b9\u6cd5\u5305\u62ec\uff1afor&#8230;in \u5faa\u73af\uff1a\u904d\u5386\u5bf9\u8c61\u7684\u952e\u548c\u503c\u3002for&#8230;of \u5faa\u73af\uff1a\u904d\u5386\u5bf9\u8c61\u7684\u5b9e\u9645\u503c\u3002object.keys() \u548c object.values()\uff1a\u5206\u522b\u904d\u5386\u5bf9\u8c61\u7684\u952e\u548c\u503c\u3002map \u5bf9\u8c61\uff1a\u5c06\u5bf9\u8c61\u8f6c\u6362\u4e3a map\uff0c\u5141\u8bb8\u904d\u5386\u952e\u503c\u5bf9\u3002\u5e93\u51fd\u6570\uff1a\u4f8b\u5982 lodash \u63d0\u4f9b\u66f4\u9ad8\u7ea7\u7684\u5faa\u73af\u65b9\u6cd5\uff0c\u5982 _.each()\u3002 \u5982\u4f55\u5728 Node.js \u4e2d\u5faa\u73af JavaScript \u5bf9\u8c61 Node.js \u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u6cd5\u6765\u5faa\u73af JavaScript \u5bf9\u8c61\uff0c\u5305\u62ec\uff1a 1. for&#8230;in \u5faa\u73af\uff1a const object = {name: &#8216;John Doe&#8217;, age: 30}; for (const key in object) { console.log(`Key: ${key}, Value: ${object[key]}`); } \u767b\u5f55\u540e\u590d\u5236 2. for&#8230;of \u5faa\u73af\uff1a const object = {name: &#8216;John Doe&#8217;, age: [&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-3418","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/3418","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=3418"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/3418\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=3418"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=3418"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=3418"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}