{"id":3159,"date":"2024-11-10T12:12:49","date_gmt":"2024-11-10T04:12:49","guid":{"rendered":"https:\/\/fwq.ai\/blog\/3159\/"},"modified":"2024-11-10T12:12:49","modified_gmt":"2024-11-10T04:12:49","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%e5%af%b9%e8%b1%a1%e6%95%b0%e7%bb%84%e5%af%b9%e8%b1%a1%e6%95%b0%e7%bb%84%e5%af%b9%e8%b1%a1","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/3159\/","title":{"rendered":"js\u5982\u4f55\u89e3\u6790\u5bf9\u8c61\u6570\u7ec4\u5bf9\u8c61\u6570\u7ec4\u5bf9\u8c61\u6570\u7ec4\u5bf9\u8c61"},"content":{"rendered":"<blockquote><p>\n  \u89e3\u6790\u5bf9\u8c61\u6570\u7ec4\u6709\u56db\u79cd\u4e3b\u8981\u65b9\u6cd5\uff1a\u4f7f\u7528 for \u5faa\u73af\u8bbf\u95ee\u6bcf\u4e2a\u5bf9\u8c61\u3002\u4f7f\u7528 foreach() \u65b9\u6cd5\u8fed\u4ee3\u6570\u7ec4\u5e76\u6267\u884c\u56de\u8c03\u51fd\u6570\u3002\u4f7f\u7528 map() \u65b9\u6cd5\u521b\u5efa\u65b0\u6570\u7ec4\uff0c\u5176\u4e2d\u6bcf\u4e2a\u5143\u7d20\u662f\u539f\u59cb\u5bf9\u8c61\u7684\u8f6c\u6362\u7ed3\u679c\u3002\u4f7f\u7528 reduce() \u65b9\u6cd5\u5c06\u6570\u7ec4\u5143\u7d20\u7d2f\u79ef\u5230\u5355\u4e2a\u503c\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/08\/2024110805393469096.jpg\" class=\"aligncenter\" title=\"js\u5982\u4f55\u89e3\u6790\u5bf9\u8c61\u6570\u7ec4\u5bf9\u8c61\u6570\u7ec4\u5bf9\u8c61\u6570\u7ec4\u5bf9\u8c61\u63d2\u56fe\" alt=\"js\u5982\u4f55\u89e3\u6790\u5bf9\u8c61\u6570\u7ec4\u5bf9\u8c61\u6570\u7ec4\u5bf9\u8c61\u6570\u7ec4\u5bf9\u8c61\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u4f7f\u7528 JavaScript \u89e3\u6790\u5bf9\u8c61\u6570\u7ec4<\/strong><\/p>\n<p>JavaScript \u4e2d\u7684\u5bf9\u8c61\u6570\u7ec4\u662f\u4e00\u79cd\u5305\u542b\u5bf9\u8c61\u7684\u6570\u7ec4\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\u6765\u89e3\u6790\u5bf9\u8c61\u6570\u7ec4\uff1a<\/p>\n<p><strong>1. \u4f7f\u7528 for \u5faa\u73af<\/strong><\/p>\n<pre>const objectArray = [\n  { name: 'John', age: 30 },\n  { name: 'Jane', age: 25 },\n];\n\nfor (let i = 0; i &lt; objectArray.length; i++) {\n  const object = objectArray[i];\n  console.log(object.name); \/\/ John, Jane\n  console.log(object.age); \/\/ 30, 25\n}<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>2. \u4f7f\u7528 forEach() \u65b9\u6cd5<\/strong><\/p>\n<pre>const objectArray = [\n  { name: 'John', age: 30 },\n  { name: 'Jane', age: 25 },\n];\n\nobjectArray.forEach((object) =&gt; {\n  console.log(object.name); \/\/ John, Jane\n  console.log(object.age); \/\/ 30, 25\n});<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>3. \u4f7f\u7528 map() \u65b9\u6cd5<\/strong><\/p>\n<pre>const objectArray = [\n  { name: 'John', age: 30 },\n  { name: 'Jane', age: 25 },\n];\n\nconst names = objectArray.map((object) =&gt; {\n  return object.name;\n});\n\nconsole.log(names); \/\/ ['John', 'Jane']<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>4. \u4f7f\u7528 reduce() \u65b9\u6cd5<\/strong><\/p>\n<pre>const objectArray = [\n  { name: 'John', age: 30 },\n  { name: 'Jane', age: 25 },\n];\n\nconst totalAge = objectArray.reduce((accumulator, object) =&gt; {\n  return accumulator + object.age;\n}, 0);\n\nconsole.log(totalAge); \/\/ 55<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fjs\u5982\u4f55\u89e3\u6790\u5bf9\u8c61\u6570\u7ec4\u5bf9\u8c61\u6570\u7ec4\u5bf9\u8c61\u6570\u7ec4\u5bf9\u8c61\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>\u89e3\u6790\u5bf9\u8c61\u6570\u7ec4\u6709\u56db\u79cd\u4e3b\u8981\u65b9\u6cd5\uff1a\u4f7f\u7528 for \u5faa\u73af\u8bbf\u95ee\u6bcf\u4e2a\u5bf9\u8c61\u3002\u4f7f\u7528 foreach() \u65b9\u6cd5\u8fed\u4ee3\u6570\u7ec4\u5e76\u6267\u884c\u56de\u8c03\u51fd\u6570\u3002\u4f7f\u7528 map() \u65b9\u6cd5\u521b\u5efa\u65b0\u6570\u7ec4\uff0c\u5176\u4e2d\u6bcf\u4e2a\u5143\u7d20\u662f\u539f\u59cb\u5bf9\u8c61\u7684\u8f6c\u6362\u7ed3\u679c\u3002\u4f7f\u7528 reduce() \u65b9\u6cd5\u5c06\u6570\u7ec4\u5143\u7d20\u7d2f\u79ef\u5230\u5355\u4e2a\u503c\u3002 \u5982\u4f55\u4f7f\u7528 JavaScript \u89e3\u6790\u5bf9\u8c61\u6570\u7ec4 JavaScript \u4e2d\u7684\u5bf9\u8c61\u6570\u7ec4\u662f\u4e00\u79cd\u5305\u542b\u5bf9\u8c61\u7684\u6570\u7ec4\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\u6765\u89e3\u6790\u5bf9\u8c61\u6570\u7ec4\uff1a 1. \u4f7f\u7528 for \u5faa\u73af const objectArray = [ { name: &#8216;John&#8217;, age: 30 }, { name: &#8216;Jane&#8217;, age: 25 }, ]; for (let i = 0; i &lt; objectArray.length; i++) { const object = objectArray[i]; console.log(object.name); \/\/ John, Jane console.log(object.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-3159","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/3159","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=3159"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/3159\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=3159"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=3159"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=3159"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}