{"id":3158,"date":"2024-11-10T08:25:15","date_gmt":"2024-11-10T00:25:15","guid":{"rendered":"https:\/\/fwq.ai\/blog\/3158\/"},"modified":"2024-11-10T08:25:15","modified_gmt":"2024-11-10T00:25:15","slug":"js%e5%a6%82%e4%bd%95%e8%a7%a3%e6%9e%90%e5%af%b9%e8%b1%a1%e6%95%b0%e7%bb%84","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/3158\/","title":{"rendered":"js\u5982\u4f55\u89e3\u6790\u5bf9\u8c61\u6570\u7ec4"},"content":{"rendered":"<blockquote><p>\n  javascript \u63d0\u4f9b\u4e24\u79cd\u65b9\u6cd5\u89e3\u6790\u5bf9\u8c61\u6570\u7ec4\uff1aforeach\uff1a\u904d\u5386\u6570\u7ec4\u5143\u7d20\uff0c\u6267\u884c\u56de\u8c03\u51fd\u6570\u4fee\u6539\u5143\u7d20\u6216\u6267\u884c\u526f\u4f5c\u7528\u3002map\uff1a\u904d\u5386\u6570\u7ec4\u5143\u7d20\uff0c\u8fd4\u56de\u7ecf\u8fc7\u56de\u8c03\u51fd\u6570\u5904\u7406\u7684\u65b0\u6570\u7ec4\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/08\/2024110805371357735.jpg\" class=\"aligncenter\" title=\"js\u5982\u4f55\u89e3\u6790\u5bf9\u8c61\u6570\u7ec4\u63d2\u56fe\" alt=\"js\u5982\u4f55\u89e3\u6790\u5bf9\u8c61\u6570\u7ec4\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u4f7f\u7528 JavaScript \u89e3\u6790\u5bf9\u8c61\u6570\u7ec4<\/strong><\/p>\n<p>JavaScript \u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u6cd5\u6765\u89e3\u6790\u5bf9\u8c61\u6570\u7ec4\uff0c\u672c\u6587\u5c06\u4ecb\u7ecd\u4e24\u79cd\u5e38\u7528\u65b9\u6cd5\uff1aforEach \u548c map\u3002<\/p>\n<p><strong>1. \u4f7f\u7528 forEach<\/strong><\/p>\n<p>forEach \u65b9\u6cd5\u7528\u4e8e\u904d\u5386\u6570\u7ec4\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u5e76\u6267\u884c\u7ed9\u5b9a\u7684\u56de\u8c03\u51fd\u6570\u3002\u8be5\u65b9\u6cd5\u4e0d\u8fd4\u56de\u4efb\u4f55\u503c\uff0c\u53ea\u80fd\u7528\u4e8e\u4fee\u6539\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u6216\u6267\u884c\u526f\u4f5c\u7528\u3002<\/p>\n<p><strong>\u8bed\u6cd5\uff1a<\/strong><\/p>\n<pre>arr.forEach(function(currentValue, index, arr) {});<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<ul>\n<li>currentValue \u662f\u6570\u7ec4\u4e2d\u5f53\u524d\u5143\u7d20\u3002<\/li>\n<li>index \u662f\u5f53\u524d\u5143\u7d20\u7684\u7d22\u5f15\u3002<\/li>\n<li>arr \u662f\u6570\u7ec4\u672c\u8eab\u3002<\/li>\n<\/ul>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>const users = [\n  { name: 'John', age: 30 },\n  { name: 'Jane', age: 25 },\n  { name: 'Bob', age: 40 },\n];\n\nusers.forEach(function(user) {\n  console.log(`Name: ${user.name}, Age: ${user.age}`);\n});<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>\u8f93\u51fa\uff1a<\/strong><\/p>\n<pre>Name: John, Age: 30\nName: Jane, Age: 25\nName: Bob, Age: 40<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>2. \u4f7f\u7528 map<\/strong><\/p>\n<p>map \u65b9\u6cd5\u4e5f\u662f\u904d\u5386\u6570\u7ec4\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u5e76\u6267\u884c\u7ed9\u5b9a\u7684\u56de\u8c03\u51fd\u6570\u3002\u4e0d\u540c\u4e8e forEach\uff0cmap \u8fd4\u56de\u4e00\u4e2a\u65b0\u6570\u7ec4\uff0c\u5176\u4e2d\u6bcf\u4e2a\u5143\u7d20\u662f\u56de\u8c03\u51fd\u6570\u7684\u8fd4\u56de\u503c\u3002<\/p>\n<p><strong>\u8bed\u6cd5\uff1a<\/strong><\/p>\n<pre>arr.map(function(currentValue, index, arr) {});<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<ul>\n<li>currentValue \u662f\u6570\u7ec4\u4e2d\u5f53\u524d\u5143\u7d20\u3002<\/li>\n<li>index \u662f\u5f53\u524d\u5143\u7d20\u7684\u7d22\u5f15\u3002<\/li>\n<li>arr \u662f\u6570\u7ec4\u672c\u8eab\u3002<\/li>\n<\/ul>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>const users = [\n  { name: 'John', age: 30 },\n  { name: 'Jane', age: 25 },\n  { name: 'Bob', age: 40 },\n];\n\nconst names = users.map(function(user) {\n  return user.name;\n});<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>\u8f93\u51fa\uff1a<\/strong><\/p>\n<pre>['John', 'Jane', 'Bob']<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u901a\u8fc7 forEach \u6216 map \u65b9\u6cd5\uff0cJavaScript \u5f00\u53d1\u4eba\u5458\u53ef\u4ee5\u8f7b\u677e\u5730\u89e3\u6790\u5bf9\u8c61\u6570\u7ec4\uff0c\u5bf9\u5176\u8fdb\u884c\u4fee\u6539\u6216\u63d0\u53d6\u6240\u9700\u7684\u4fe1\u606f\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fjs\u5982\u4f55\u89e3\u6790\u5bf9\u8c61\u6570\u7ec4\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>javascript \u63d0\u4f9b\u4e24\u79cd\u65b9\u6cd5\u89e3\u6790\u5bf9\u8c61\u6570\u7ec4\uff1aforeach\uff1a\u904d\u5386\u6570\u7ec4\u5143\u7d20\uff0c\u6267\u884c\u56de\u8c03\u51fd\u6570\u4fee\u6539\u5143\u7d20\u6216\u6267\u884c\u526f\u4f5c\u7528\u3002map\uff1a\u904d\u5386\u6570\u7ec4\u5143\u7d20\uff0c\u8fd4\u56de\u7ecf\u8fc7\u56de\u8c03\u51fd\u6570\u5904\u7406\u7684\u65b0\u6570\u7ec4\u3002 \u5982\u4f55\u4f7f\u7528 JavaScript \u89e3\u6790\u5bf9\u8c61\u6570\u7ec4 JavaScript \u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u6cd5\u6765\u89e3\u6790\u5bf9\u8c61\u6570\u7ec4\uff0c\u672c\u6587\u5c06\u4ecb\u7ecd\u4e24\u79cd\u5e38\u7528\u65b9\u6cd5\uff1aforEach \u548c map\u3002 1. \u4f7f\u7528 forEach forEach \u65b9\u6cd5\u7528\u4e8e\u904d\u5386\u6570\u7ec4\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u5e76\u6267\u884c\u7ed9\u5b9a\u7684\u56de\u8c03\u51fd\u6570\u3002\u8be5\u65b9\u6cd5\u4e0d\u8fd4\u56de\u4efb\u4f55\u503c\uff0c\u53ea\u80fd\u7528\u4e8e\u4fee\u6539\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u6216\u6267\u884c\u526f\u4f5c\u7528\u3002 \u8bed\u6cd5\uff1a arr.forEach(function(currentValue, index, arr) {}); \u767b\u5f55\u540e\u590d\u5236 currentValue \u662f\u6570\u7ec4\u4e2d\u5f53\u524d\u5143\u7d20\u3002 index \u662f\u5f53\u524d\u5143\u7d20\u7684\u7d22\u5f15\u3002 arr \u662f\u6570\u7ec4\u672c\u8eab\u3002 \u793a\u4f8b\uff1a const users = [ { name: &#8216;John&#8217;, age: 30 }, { name: &#8216;Jane&#8217;, age: 25 }, { name: &#8216;Bob&#8217;, age: 40 }, ]; users.forEach(function(user) { console.log(`Name: ${user.name}, 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-3158","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/3158","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=3158"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/3158\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=3158"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=3158"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=3158"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}