{"id":392,"date":"2023-08-09T14:51:07","date_gmt":"2023-08-09T06:51:07","guid":{"rendered":"https:\/\/iwebg.cn\/?p=392"},"modified":"2023-08-09T15:35:47","modified_gmt":"2023-08-09T07:35:47","slug":"%e8%a7%a3%e5%86%b3js%e4%b8%adforeach%e6%97%a0%e6%b3%95%e9%80%80%e5%87%ba%e5%be%aa%e7%8e%af%e9%97%ae%e9%a2%98","status":"publish","type":"post","link":"http:\/\/iwebg.cn\/index.php\/2023\/08\/09\/%e8%a7%a3%e5%86%b3js%e4%b8%adforeach%e6%97%a0%e6%b3%95%e9%80%80%e5%87%ba%e5%be%aa%e7%8e%af%e9%97%ae%e9%a2%98\/","title":{"rendered":"\u89e3\u51b3js\u4e2dforEach\u65e0\u6cd5\u9000\u51fa\u5faa\u73af\u95ee\u9898"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">forEach\u4e2d\u4f7f\u7528break\u548creturn\u7684\u6548\u679c<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>    \/\/ \u4f7f\u7528break\n    let arr = &#91;'1', '2', '3', '4', '5'];\r\n    arr.forEach(item=>{\r\n      if(item == '3') {\r\n        break\r\n      }\r\n      console.log(item) \/\/ \u7ed3\u679c\uff1a\u62a5\u9519 Uncaught SyntaxError: Illegal break statement at Array.forEach (&lt;anonymous>)\r\n    })\n    \/\/ \u4f7f\u7528 return\n    let arr = &#91;'1', '2', '3', '4', '5'];\r\n    arr.forEach(item=>{\r\n      if(item == '3') {\r\n        return\r\n      }\r\n      console.log(item) \/\/ \u7ed3\u679c\uff1a1 2 4 5 \u53ea\u80fd\u7ec8\u6b62\u672c\u6b21\u5faa\u73af\uff0c\u6267\u884c\u4e0b\u4e00\u6b21\u5faa\u73af\uff0c\u5e76\u4e0d\u80fd\u8df3\u51fa\u5faa\u73af\r\n    })\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">forEach\u7684\u5e95\u5c42\u5b9e\u73b0<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><code>forEach<\/code>\u65b9\u6cd5\u65e0\u6cd5\u88ab\u76f4\u63a5\u7ec8\u6b62\u7684\u539f\u56e0\u662f\u5b83\u662f\u4e00\u4e2a<strong>\u8fed\u4ee3\u5668\uff08iterator\uff09<\/strong>\u65b9\u6cd5\uff0c\u5b83\u4f1a\u904d\u5386\u6570\u7ec4\u7684\u6bcf\u4e2a\u5143\u7d20\u5e76\u4f9d\u6b21\u6267\u884c\u56de\u8c03\u51fd\u6570\uff0c\u5e95\u5c42\u5b9e\u73b0\u4e2d\uff0c\u5e76\u6ca1\u6709\u63d0\u4f9b\u5728\u5faa\u73af\u8fc7\u7a0b\u4e2d\u7ec8\u6b62\u7684\u673a\u5236\u3002<\/p>\n\n\n\n<div class=\"wp-block-group\"><div class=\"wp-block-group__inner-container is-layout-constrained wp-block-group-is-layout-constrained\">\n<h3 class=\"wp-block-heading\">\u8fed\u4ee3\u5668\uff08iterator\uff09\uff1a<\/h3>\n\n\n\n<div class=\"wp-block-group\"><div class=\"wp-block-group__inner-container is-layout-constrained wp-block-group-is-layout-constrained\">\n<p class=\"has-small-font-size wp-block-paragraph\">\u8fed\u4ee3\u5668\uff08Iterator\uff09\u662f\u4e00\u79cd\u5bf9\u8c61\uff0c\u5b83\u63d0\u4f9b\u4e00\u79cd\u7528\u4e8e\u6309\u987a\u5e8f\u8bbf\u95ee\u96c6\u5408\uff08\u5982\u6570\u7ec4\u3001\u5b57\u7b26\u4e32\u3001Set\u3001Map \u7b49\uff09\u4e2d\u6bcf\u4e2a\u5143\u7d20\u7684\u65b9\u6cd5\u3002\u8fed\u4ee3\u5668\u4e3b\u8981\u6709\u4e24\u4e2a\u5173\u952e\u65b9\u6cd5\uff1a<code>next()<\/code> \u548c <code>[Symbol.iterator]()<\/code>\u3002<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li class=\"has-small-font-size\"><code>next()<\/code> \u65b9\u6cd5\uff1a\u5f53\u8c03\u7528\u8fed\u4ee3\u5668\u7684 <code>next()<\/code> \u65b9\u6cd5\u65f6\uff0c\u5b83\u4f1a\u8fd4\u56de\u4e00\u4e2a\u5305\u542b\u4e24\u4e2a\u5c5e\u6027\u7684\u5bf9\u8c61\uff0c\u5373 <code>{ value, done }<\/code>\u3002\u5176\u4e2d\uff0c\n<ul class=\"wp-block-list\">\n<li><code>value<\/code>&nbsp;\u8868\u793a\u5f53\u524d\u8fed\u4ee3\u4f4d\u7f6e\u7684\u503c\u3002<\/li>\n\n\n\n<li><code>done<\/code>&nbsp;\u662f\u4e00\u4e2a\u5e03\u5c14\u503c\uff0c\u8868\u793a\u8fed\u4ee3\u662f\u5426\u5df2\u7ecf\u7ed3\u675f\u3002\u5982\u679c\u8fed\u4ee3\u5c1a\u672a\u7ed3\u675f\uff0c\u5219&nbsp;<code>done<\/code>&nbsp;\u4e3a&nbsp;<code>false<\/code>\uff1b\u5982\u679c\u8fed\u4ee3\u5df2\u7ecf\u5b8c\u6210\uff0c\u5219&nbsp;<code>done<\/code>&nbsp;\u4e3a&nbsp;<code>true<\/code>\u3002<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li class=\"has-small-font-size\"><code>[Symbol.iterator]()<\/code> \u65b9\u6cd5\uff1a\u8be5\u65b9\u6cd5\u8fd4\u56de\u8fed\u4ee3\u5668\u5bf9\u8c61\u81ea\u8eab\uff0c\u4ee5\u5b9e\u73b0\u53ef\u8fed\u4ee3\u534f\u8bae\u3002\u8fd9\u4f7f\u5f97\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528 <code>for...of<\/code> \u5faa\u73af\u6216\u901a\u8fc7\u8c03\u7528 <code>Array.from()<\/code>\u3001<code>spread<\/code> \u64cd\u4f5c\u7b26\u7b49\u65b9\u5f0f\u6765\u8fed\u4ee3\u96c6\u5408\u4e2d\u7684\u5143\u7d20\u3002<\/li>\n<\/ol>\n\n\n\n<p class=\"has-small-font-size wp-block-paragraph\">\u8fed\u4ee3\u5668\u7684\u4f18\u70b9\u5728\u4e8e\u5b83\u63d0\u4f9b\u4e86\u4e00\u79cd\u901a\u7528\u7684\u8bbf\u95ee\u96c6\u5408\u5143\u7d20\u7684\u65b9\u5f0f\uff0c\u65e0\u8bba\u96c6\u5408\u7684\u5185\u90e8\u7ed3\u6784\u662f\u4ec0\u4e48\u6837\u7684\u3002\u8fed\u4ee3\u5668\u53ef\u4ee5\u6839\u636e\u96c6\u5408\u7684\u4e0d\u540c\u7c7b\u578b\u548c\u7ed3\u6784\uff0c\u63d0\u4f9b\u9002\u5f53\u7684\u7b97\u6cd5\u6765\u904d\u5386\u96c6\u5408\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\uff0c\u4f7f\u5f97\u6211\u4eec\u53ef\u4ee5\u8f7b\u677e\u5730\u5904\u7406\u96c6\u5408\u7684\u5143\u7d20\uff0c\u6267\u884c\u5404\u79cd\u64cd\u4f5c\uff0c\u4f8b\u5982\u8fc7\u6ee4\u3001\u6620\u5c04\u3001\u67e5\u627e\u7b49\u3002<\/p>\n<\/div><\/div>\n<\/div><\/div>\n\n\n\n<p class=\"wp-block-paragraph\"><code>forEach<\/code>\u5185\u90e8\u4f7f\u7528\u4e86\u4e00\u4e2a\u5faa\u73af\u6765\u904d\u5386\u6570\u7ec4\uff0c\u4f46\u5b83\u5e76\u4e0d\u50cf\u5e38\u89c4\u7684\u5faa\u73af\uff08\u5982<code>for<\/code>\u5faa\u73af\uff09\u90a3\u6837\u5177\u6709\u53ef\u4ee5\u88ab\u63a7\u5236\u7684\u8fed\u4ee3\u6761\u4ef6\u6216\u8df3\u51fa\u673a\u5236\u3002\u5f53\u4f60\u8c03\u7528<code>forEach<\/code>\u65f6\uff0c\u5b83\u5c06\u4f1a\u6309\u987a\u5e8f\u904d\u5386\u6574\u4e2a\u6570\u7ec4\uff0c\u5e76\u901a\u8fc7\u4f9d\u6b21\u8c03\u7528\u6307\u5b9a\u7684\u56de\u8c03\u51fd\u6570\u6765\u5904\u7406\u6bcf\u4e2a\u5143\u7d20\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u7531\u4e8e<code>forEach<\/code>\u65b9\u6cd5\u662f\u540c\u6b65\u6267\u884c\u7684\uff0c\u5b83\u4f1a\u7b49\u5f85\u6bcf\u4e2a\u56de\u8c03\u51fd\u6570\u7684\u6267\u884c\u5b8c\u6210\u540e\u518d\u7ee7\u7eed\u904d\u5386\u4e0b\u4e00\u4e2a\u5143\u7d20\u3002\u8fd9\u610f\u5473\u7740\uff0c\u5373\u4f7f\u5728\u56de\u8c03\u51fd\u6570\u4e2d\u5c1d\u8bd5\u4f7f\u7528<code>return<\/code>\u8bed\u53e5\u6216<code>break<\/code>\u8bed\u53e5\uff0c\u4e5f\u65e0\u6cd5\u771f\u6b63\u4e2d\u65ad\u6216\u8df3\u51fa\u6574\u4e2a<code>forEach<\/code>\u5faa\u73af\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u6a21\u62df\u5b9e\u73b0<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>Array.prototype.forEachX = function(callback) {\n  const iterator = this&#91;Symbol.iterator]();\n  let current = iterator.next();\n\n  while (!current.done) {\n    callback(current.value);\n    current = iterator.next();\n  }\n};<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u8fd9\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u83b7\u53d6\u6570\u7ec4\u7684\u8fed\u4ee3\u5668 <code>iterator<\/code>\uff0c\u901a\u8fc7 <code>this[Symbol.iterator]()<\/code> \u65b9\u6cd5\u6765\u83b7\u53d6\u3002\u7136\u540e\uff0c\u6211\u4eec\u4f7f\u7528 <code>while<\/code> \u5faa\u73af\u904d\u5386\u8fed\u4ee3\u5668\u3002\u5728\u6bcf\u6b21\u5faa\u73af\u4e2d\uff0c\u6211\u4eec\u8c03\u7528\u56de\u8c03\u51fd\u6570 <code>callback<\/code> \u6765\u5904\u7406\u5f53\u524d\u8fed\u4ee3\u7684\u503c <code>current.value<\/code>\u3002\u5faa\u73af\u4f1a\u4e00\u76f4\u8fdb\u884c\uff0c\u76f4\u5230\u8fed\u4ee3\u5668\u7684 <code>next<\/code> \u65b9\u6cd5\u8fd4\u56de\u7684\u7ed3\u679c\u4e2d\u7684 <code>done<\/code> \u5c5e\u6027\u4e3a <code>true<\/code>\uff0c\u8868\u793a\u8fed\u4ee3\u5df2\u7ecf\u7ed3\u675f\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u6211\u4eec\u53ef\u4ee5\u505a\u4e00\u4e2a\u7b80\u5355\u7684demo\u6765\u8fdb\u884c\u67e5\u770b<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Array.prototype.forEachx = function(callback) {\n  const iterator = this&#91;Symbol.iterator]();\n  let current = iterator.next();\n  console.log(current);\n  while (!current.done) {\n    const result = callback(current.value);\n    console.log(result)  \n    current = iterator.next();\n  }\n};\n\nconst myArray = &#91;1, 2, 3, 4, 5];\nmyArray.forEachx((item) => {\n  console.log(item);\n  return '\u6211\u662freturn'\n})\n<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='https:\/\/iwebg.cn\/wp-content\/uploads\/2023\/08\/image-5.png'><img class=\"lazyload lazyload-style-1\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  loading=\"lazy\" decoding=\"async\" width=\"449\" height=\"481\" data-original=\"https:\/\/iwebg.cn\/wp-content\/uploads\/2023\/08\/image-5.png\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"\" class=\"wp-image-398\"  sizes=\"auto, (max-width: 449px) 100vw, 449px\" \/><\/div><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u53ef\u4ee5\u770b\u5230\u8fd9\u91cc\u6211\u4eec\u5b9e\u73b0\u4e86\u4e00\u4e2a\u7b80\u5355\u7684forEach\u867d\u7136\u6211\u4eec<code>return<\/code>\u4e86 \u4f46\u662f\u5176\u5b9e\u53ea\u662f<code>return<\/code>\u5230\u4e86 <code>callback(current.value)<\/code> \u8fd9\u91cc \u7ed9\u4e86 result\u4e00\u4e2a\u8fd4\u56de\u503c \u5e76\u4e0d\u4f1a\u771f\u6b63\u7684\u7ec8\u6b62<code>while<\/code>\u5faa\u73af\u3002\u5982\u679c\u9700\u8981\u5b9e\u9645\u5bf9<code>while<\/code>\u503c\u8fdb\u884c\u7ec8\u6b62\u6211\u4eec\u53ef\u4ee5\u52a0\u5165\u4e00\u4e2a\u5224\u65ad \u5f53\u68c0\u6d4b\u5230 <code>result === 'xxx'<\/code> \u7684\u65f6\u5019 <code>break<\/code> \u8fd9\u4e2a<code>while<\/code>\u5faa\u73af \u8fd9\u6837\u4ed6\u5c31\u4e0d\u4f1a\u53bb\u6267\u884c<code> iterator.next(); <\/code>\u8fd9\u4e2a\u8fed\u4ee3\u5668\u4e5f\u5c31\u7ec8\u6b62\u6389\u4e86\uff0c\u5b9e\u73b0\u5982\u4e0b:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='https:\/\/iwebg.cn\/wp-content\/uploads\/2023\/08\/image-6.png'><img class=\"lazyload lazyload-style-1\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  loading=\"lazy\" decoding=\"async\" width=\"383\" height=\"305\" data-original=\"https:\/\/iwebg.cn\/wp-content\/uploads\/2023\/08\/image-6.png\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"\" class=\"wp-image-399\"  sizes=\"auto, (max-width: 383px) 100vw, 383px\" \/><\/div><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">\u89e3\u51b3\u65b9\u6848<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u9996\u5148\uff0c\u6211\u4eec\u4e0d\u53ef\u80fd\u81ea\u5df1\u5199\u4e00\u4e2a\u8fed\u4ee3\u5668\u53bb\u66ff\u6362\u539f\u672c\u7684forEach\uff0c\u9664\u975e\u4f60\u662f<strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-luminous-vivid-amber-color\">\u6700\u5f3a\u738b\u8005<\/mark><\/strong>\uff0c\u6240\u4ee5\u6211\u4eec\u4e00\u822c\u4f7f\u7528\u5982\u4e0b\u7684\u89e3\u51b3\u65b9\u6848\u6765\u89e3\u51b3\u5faa\u73af\u4e2d\u9700\u8981\u8df3\u8fc7\u7684\u95ee\u9898\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u901a\u8fc7for\u5faa\u73af\u6765\u5b9e\u73b0<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>     let arr = &#91;'1', '2', '3', '4', '5'];\n     for(let i=0; i&lt;arr.length; i++) {\n       if(arr&#91;i] == '3') break;\n       console.log(arr&#91;i]) \/\/ \u5f97\u5230\u7ed3\u679c 1 2\n     }<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u901a\u8fc7try\u00b7\u00b7\u00b7catch\u629b\u51fa\u5f02\u5e38\u7684\u65b9\u5f0f\u5b9e\u73b0<\/strong><\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>    let arr = &#91;'1', '2', '3', '4', '5'];\r\n\r\n    try {\r\n      arr.forEach((item, index) => {\r\n        if (item === '3') {\r\n          throw new Error('End');\r\n        }\r\n        console.log(item); \/\/ 1 2\r\n      });\r\n    } catch (e) {\r\n    if (e.message === 'End') {\r\n    \/\/ \u5904\u7406\u5f02\u5e38\r\n        console.error(e);\r\n    } else {\r\n      throw e;\r\n    }\r\n   }\r\n<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u901a\u8fc7reduce\u8fed\u4ee3\u5b9e\u73b0\uff0c\u9700\u8bbe\u7f6e\u4e00\u4e2a\u4e2d\u65adflag<\/strong><\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>   let arr = &#91;'1', '2', '3', '4', '5'];\r\n    arr.reduce(function (p, c) {\r\n      if (c == '3') {\r\n        this.break = true\r\n      }\r\n      if (this.break) {\r\n        return\r\n      }\r\n      console.log(c) \/\/ 1 2\r\n    }, '') <\/code><\/pre>\n\n\n\n<div class=\"wp-block-group has-small-font-size\"><div class=\"wp-block-group__inner-container is-layout-constrained wp-block-group-is-layout-constrained\">\n<p class=\"wp-block-paragraph\"><code>Array.prototype.reduce<\/code> \u662f JavaScript \u4e2d\u7528\u4e8e\u8fed\u4ee3\u6570\u7ec4\u7684\u65b9\u6cd5\u4e4b\u4e00\u3002\u5b83\u53ef\u4ee5\u5bf9\u6570\u7ec4\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u8fdb\u884c\u7d2f\u52a0\u3001\u8ba1\u7b97\u6216\u8f6c\u6362\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2a\u6700\u7ec8\u7ed3\u679c\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>Array.prototype.reduce<\/code> \u65b9\u6cd5\u63a5\u53d7\u4e00\u4e2a\u56de\u8c03\u51fd\u6570\u4f5c\u4e3a\u53c2\u6570\uff0c\u5e76\u4e14\u53ef\u4ee5\u4f20\u9012\u4e00\u4e2a\u521d\u59cb\u503c\u4f5c\u4e3a\u53ef\u9009\u7684\u7b2c\u4e8c\u4e2a\u53c2\u6570\u3002\u56de\u8c03\u51fd\u6570\u63a5\u53d7\u56db\u4e2a\u53c2\u6570\uff1a\u7d2f\u52a0\u5668\uff08accumulator\uff09\u3001\u5f53\u524d\u503c\uff08current value\uff09\u3001\u5f53\u524d\u7d22\u5f15\uff08current index\uff09\u548c\u539f\u59cb\u6570\u7ec4\uff08original array\uff09\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e0b\u9762\u662f <code>Array.prototype.reduce<\/code> \u65b9\u6cd5\u7684\u57fa\u672c\u8bed\u6cd5\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>arr.reduce(callback, initialValue);<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>arr<\/code>\uff1a\u8981\u8fdb\u884c\u8fed\u4ee3\u7684\u6570\u7ec4\u3002<\/li>\n\n\n\n<li><code>callback<\/code>\uff1a\u56de\u8c03\u51fd\u6570\uff0c\u7528\u4e8e\u5bf9\u6bcf\u4e2a\u5143\u7d20\u8fdb\u884c\u64cd\u4f5c\u3002\u5b83\u53ef\u4ee5\u63a5\u53d7\u56db\u4e2a\u53c2\u6570\uff1a\u7d2f\u52a0\u5668\u3001\u5f53\u524d\u503c\u3001\u5f53\u524d\u7d22\u5f15\u548c\u539f\u59cb\u6570\u7ec4\u3002<\/li>\n\n\n\n<li><code>initialValue<\/code>\uff1a\u53ef\u9009\u53c2\u6570\uff0c\u7528\u4f5c\u521d\u59cb\u503c\u3002<\/li>\n<\/ul>\n<\/div><\/div>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u6570\u7ec4\u7684\u53e6\u5916\u4e24\u4e2a\u65b9\u6cd5some\u4e0eevery\uff08\u4e0d\u5efa\u8bae\u4f7f\u7528\uff09<\/strong><\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>    let arr = &#91;'1', '2', '3', '4', '5'];\n    arr.some(item=> {\n      if (item == '3') {\n        return true;\n      }\n      console.log(item); \/\/ 1 2 true\n    });\n    let arr = &#91;'1', '2', '3', '4', '5'];\r\n    arr.every(item=> {\r\n      if (item == '3') {\r\n        return false;\r\n      } else {\r\n        console.log(item);\r\n        return true;\r\n      }\r\n    }); \/\/ 1 2 false<\/code><\/pre>\n\n\n\n<p class=\"has-small-font-size wp-block-paragraph\"><code>Array.prototype.some(callback)<\/code> \u65b9\u6cd5\u4f1a\u5bf9\u6570\u7ec4\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u6267\u884c\u63d0\u4f9b\u7684\u56de\u8c03\u51fd\u6570\uff0c\u76f4\u5230\u627e\u5230\u4e00\u4e2a\u4f7f\u5f97\u56de\u8c03\u51fd\u6570\u8fd4\u56de <code>true<\/code> \u7684\u5143\u7d20\u3002\u5982\u679c\u627e\u5230\u8fd9\u6837\u7684\u5143\u7d20\uff0c<code>some<\/code> \u65b9\u6cd5\u5c31\u4f1a\u7acb\u5373\u8fd4\u56de <code>true<\/code>\uff0c\u5e76\u505c\u6b62\u7ee7\u7eed\u8fed\u4ee3\u3002\u5982\u679c\u6570\u7ec4\u4e2d\u6ca1\u6709\u7b26\u5408\u6761\u4ef6\u7684\u5143\u7d20\uff0c<code>some<\/code> \u65b9\u6cd5\u6700\u540e\u4f1a\u8fd4\u56de <code>false<\/code>\u3002<\/p>\n\n\n\n<p class=\"has-small-font-size wp-block-paragraph\"><code>Array.prototype.every(callback)<\/code> \u65b9\u6cd5\u4f1a\u5bf9\u6570\u7ec4\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u6267\u884c\u63d0\u4f9b\u7684\u56de\u8c03\u51fd\u6570\uff0c\u53ea\u6709\u5f53\u6240\u6709\u5143\u7d20\u90fd\u4f7f\u5f97\u56de\u8c03\u51fd\u6570\u8fd4\u56de <code>true<\/code> \u65f6\uff0c<code>every<\/code> \u65b9\u6cd5\u624d\u4f1a\u8fd4\u56de <code>true<\/code>\u3002\u5982\u679c\u6709\u4efb\u4f55\u4e00\u4e2a\u5143\u7d20\u4f7f\u5f97\u56de\u8c03\u51fd\u6570\u8fd4\u56de <code>false<\/code>\uff0c<code>every<\/code> \u65b9\u6cd5\u5c31\u4f1a\u7acb\u5373\u8fd4\u56de <code>false<\/code>\uff0c\u5e76\u505c\u6b62\u7ee7\u7eed\u8fed\u4ee3\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>forEach\u4e2d\u4f7f\u7528break\u548creturn\u7684\u6548\u679c forEach\u7684\u5e95\u5c42\u5b9e\u73b0 forEach\u65b9\u6cd5\u65e0\u6cd5\u88ab\u76f4\u63a5\u7ec8\u6b62 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[18],"tags":[6],"class_list":["post-392","post","type-post","status-publish","format-standard","hentry","category-javascript","tag-js"],"_links":{"self":[{"href":"http:\/\/iwebg.cn\/index.php\/wp-json\/wp\/v2\/posts\/392","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/iwebg.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/iwebg.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/iwebg.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/iwebg.cn\/index.php\/wp-json\/wp\/v2\/comments?post=392"}],"version-history":[{"count":7,"href":"http:\/\/iwebg.cn\/index.php\/wp-json\/wp\/v2\/posts\/392\/revisions"}],"predecessor-version":[{"id":406,"href":"http:\/\/iwebg.cn\/index.php\/wp-json\/wp\/v2\/posts\/392\/revisions\/406"}],"wp:attachment":[{"href":"http:\/\/iwebg.cn\/index.php\/wp-json\/wp\/v2\/media?parent=392"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/iwebg.cn\/index.php\/wp-json\/wp\/v2\/categories?post=392"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/iwebg.cn\/index.php\/wp-json\/wp\/v2\/tags?post=392"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}