{"id":1027,"date":"2023-03-12T09:37:19","date_gmt":"2023-03-12T01:37:19","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=1027"},"modified":"2023-04-29T13:13:07","modified_gmt":"2023-04-29T05:13:07","slug":"foreach-and-each-in-javascript","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/03\/12\/foreach-and-each-in-javascript\/","title":{"rendered":"JavaScript\u4e2dforEach\u4e0eeach"},"content":{"rendered":"<p>forEach\u662fES5\u4e2d\u64cd\u4f5c\u6570\u7ec4\u7684\u4e00\u79cd\u65b9\u6cd5\uff0c\u4e3b\u8981\u529f\u80fd\u662f\u904d\u5386\u6570\u7ec4\uff0c\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-javascript\">var arr = [1, 2, 3, 4];\narr.forEach(alert);<\/code><\/pre>\n<p><!-- more --><\/p>\n<p>\u7b49\u4ef7\u4e8e\uff1a<\/p>\n<pre><code class=\"language-javascript\">var arr = [1, 2, 3, 4];\nfor (var k = 0, length = arr.length; k &lt; length; k++) {\n  alert(array[k]);\n}<\/code><\/pre>\n<p>forEach\u65b9\u6cd5\u4e2d\u7684function\u56de\u8c03\u6709\u4e09\u4e2a\u53c2\u6570\uff1a\u7b2c\u4e00\u4e2a\u53c2\u6570\u662f\u904d\u5386\u7684\u6570\u7ec4\u5185\u5bb9\uff0c\u7b2c\u4e8c\u4e2a\u53c2\u6570\u662f\u5bf9\u5e94\u7684\u6570\u7ec4\u7d22\u5f15\uff0c\u7b2c\u4e09\u4e2a\u53c2\u6570\u662f\u6570\u7ec4\u672c\u8eab\u3002\u56e0\u6b64\uff1a<\/p>\n<pre><code class=\"language-javascript\">[].forEach(function(value, index, array) {\n  \/\/code something\n\n});<\/code><\/pre>\n<p>\u7b49\u4ef7\u4e8e\uff1a<\/p>\n<pre><code class=\"language-javascript\">$.each([], function(index, value, array) {\n  \/\/code something\n\n})<\/code><\/pre>\n<p>\u5199\u4e00\u4e2a\u4f8b\u5b50\uff1a<\/p>\n<pre><code class=\"language-javascript\">var arr = [1,2,3,4];\narr.forEach(function(value, index, array) {\n  array[index] == value;  \/\/\u7ed3\u679c\u4e3atrue\n  sum += value;  \n});\nconsole.log(sum);  \/\/\u7ed3\u679c\u4e3a10<\/code><\/pre>\n<p>map\uff1amap\u5373\u662f\u201c\u6620\u5c04\u201d\u7684\u610f\u601d\uff0c\u7528\u6cd5\u4e0eforEach\u76f8\u4f3c\uff0c\u7528\u6cd5\u5373\uff1a<\/p>\n<pre><code class=\"language-javascript\">[].map(function(value, index, array) {\n  \/\/code something\n\n})<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>forEach\u662fES5\u4e2d\u64cd\u4f5c\u6570\u7ec4\u7684\u4e00\u79cd\u65b9\u6cd5\uff0c\u4e3b\u8981\u529f\u80fd\u662f\u904d\u5386\u6570\u7ec4\uff0c\u4f8b\u5982\uff1a var arr = [1, 2, 3, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[33],"tags":[84],"class_list":["post-1027","post","type-post","status-publish","format-standard","hentry","category-frontend-basic","tag-javascript"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1027","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=1027"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1027\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=1027"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=1027"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=1027"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}