{"id":532,"date":"2025-12-05T19:45:23","date_gmt":"2025-12-05T11:45:23","guid":{"rendered":"https:\/\/www.52runoob.com\/?p=532"},"modified":"2025-12-05T19:45:23","modified_gmt":"2025-12-05T11:45:23","slug":"javascript%e6%95%b0%e7%bb%84%e5%b8%b8%e7%94%a8%e6%96%b9%e6%b3%95find%e3%80%81findindex%e3%80%81filter%e3%80%81map%e3%80%81flatmap%e5%8f%8asome%e8%af%a6%e8%a7%a3","status":"publish","type":"post","link":"https:\/\/www.52runoob.com\/index.php\/2025\/12\/05\/javascript%e6%95%b0%e7%bb%84%e5%b8%b8%e7%94%a8%e6%96%b9%e6%b3%95find%e3%80%81findindex%e3%80%81filter%e3%80%81map%e3%80%81flatmap%e5%8f%8asome%e8%af%a6%e8%a7%a3\/","title":{"rendered":"JavaScript\u6570\u7ec4\u5e38\u7528\u65b9\u6cd5find\u3001findIndex\u3001filter\u3001map\u3001flatMap\u53casome\u8be6\u89e3"},"content":{"rendered":"\n<p>\u4e0b\u9762\u7ed9\u4f60\u6574\u7406\u4e00\u4efd <strong>JavaScript \u6570\u7ec4\u5e38\u7528\u65b9\u6cd5\u8be6\u89e3<\/strong>\uff0c\u5305\u62ec <code>find<\/code>\u3001<code>findIndex<\/code>\u3001<code>filter<\/code>\u3001<code>map<\/code>\u3001<code>flatMap<\/code> \u548c <code>some<\/code>\uff0c\u6bcf\u4e2a\u65b9\u6cd5\u914d\u5408 <strong>\u539f\u7406\u8bf4\u660e + \u4f7f\u7528\u793a\u4f8b + \u6ce8\u610f\u4e8b\u9879<\/strong>\uff0c\u9002\u5408\u65b0\u624b\u548c\u8fdb\u9636\u590d\u4e60\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\ud83d\udd25 1\ufe0f\u20e3 <code>find<\/code> \u65b9\u6cd5<\/h1>\n\n\n\n<p><strong>\u4f5c\u7528\uff1a<\/strong><br>\u8fd4\u56de <strong>\u6570\u7ec4\u4e2d\u7b2c\u4e00\u4e2a\u7b26\u5408\u6761\u4ef6\u7684\u5143\u7d20<\/strong>\uff0c\u5982\u679c\u6ca1\u6709\u7b26\u5408\u6761\u4ef6\u7684\u8fd4\u56de <code>undefined<\/code>\u3002<\/p>\n\n\n\n<p><strong>\u8bed\u6cd5\uff1a<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\narr.find(callback(element, index, array))\n\n<\/pre><\/div>\n\n\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nconst users = &#x5B;\n  { id: 1, name: &#039;Alice&#039; },\n  { id: 2, name: &#039;Bob&#039; },\n  { id: 3, name: &#039;Charlie&#039; }\n];\n\nconst user = users.find(u =&gt; u.id === 2);\nconsole.log(user); \/\/ { id: 2, name: &#039;Bob&#039; }\n\n<\/pre><\/div>\n\n\n<p><strong>\u6ce8\u610f\u4e8b\u9879\uff1a<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u53ea\u8fd4\u56de\u7b2c\u4e00\u4e2a\u7b26\u5408\u6761\u4ef6\u7684\u5143\u7d20<\/li>\n\n\n\n<li>\u56de\u8c03\u51fd\u6570\u8fd4\u56de <code>true<\/code> \u65f6\u505c\u6b62\u904d\u5386<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\ud83d\udd25 2\ufe0f\u20e3 <code>findIndex<\/code> \u65b9\u6cd5<\/h1>\n\n\n\n<p><strong>\u4f5c\u7528\uff1a<\/strong><br>\u8fd4\u56de <strong>\u6570\u7ec4\u4e2d\u7b2c\u4e00\u4e2a\u7b26\u5408\u6761\u4ef6\u7684\u5143\u7d20\u7d22\u5f15<\/strong>\uff0c\u627e\u4e0d\u5230\u8fd4\u56de <code>-1<\/code>\u3002<\/p>\n\n\n\n<p><strong>\u8bed\u6cd5\uff1a<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\narr.findIndex(callback(element, index, array))\n\n<\/pre><\/div>\n\n\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nconst index = users.findIndex(u =&gt; u.name === &#039;Charlie&#039;);\nconsole.log(index); \/\/ 2\n\n<\/pre><\/div>\n\n\n<p><strong>\u6ce8\u610f\u4e8b\u9879\uff1a<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u4e0e <code>find<\/code> \u7c7b\u4f3c\uff0c\u4f46\u8fd4\u56de\u7d22\u5f15<\/li>\n\n\n\n<li>\u7528\u4e8e\u5220\u9664\u6216\u66f4\u65b0\u7279\u5b9a\u5143\u7d20<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\ud83d\udd25 3\ufe0f\u20e3 <code>filter<\/code> \u65b9\u6cd5<\/h1>\n\n\n\n<p><strong>\u4f5c\u7528\uff1a<\/strong><br>\u8fd4\u56de <strong>\u6240\u6709\u7b26\u5408\u6761\u4ef6\u7684\u5143\u7d20\u7ec4\u6210\u7684\u65b0\u6570\u7ec4<\/strong>\u3002<\/p>\n\n\n\n<p><strong>\u8bed\u6cd5\uff1a<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\narr.filter(callback(element, index, array))\n\n<\/pre><\/div>\n\n\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nconst numbers = &#x5B;1, 2, 3, 4, 5, 6];\nconst evenNumbers = numbers.filter(n =&gt; n % 2 === 0);\nconsole.log(evenNumbers); \/\/ &#x5B;2, 4, 6]\n\n<\/pre><\/div>\n\n\n<p><strong>\u6ce8\u610f\u4e8b\u9879\uff1a<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u4e0d\u6539\u53d8\u539f\u6570\u7ec4<\/li>\n\n\n\n<li>\u56de\u8c03\u8fd4\u56de <code>true<\/code> \u7684\u5143\u7d20\u624d\u4f1a\u88ab\u4fdd\u7559<\/li>\n\n\n\n<li>\u8fd4\u56de\u6570\u7ec4\u957f\u5ea6\u53ef\u4e3a 0<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\ud83d\udd25 4\ufe0f\u20e3 <code>map<\/code> \u65b9\u6cd5<\/h1>\n\n\n\n<p><strong>\u4f5c\u7528\uff1a<\/strong><br>\u5bf9\u6570\u7ec4\u6bcf\u4e2a\u5143\u7d20\u6267\u884c\u56de\u8c03\uff0c\u5e76\u8fd4\u56de <strong>\u65b0\u6570\u7ec4<\/strong>\uff0c\u957f\u5ea6\u4e0e\u539f\u6570\u7ec4\u76f8\u540c\u3002<\/p>\n\n\n\n<p><strong>\u8bed\u6cd5\uff1a<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\narr.map(callback(element, index, array))\n\n<\/pre><\/div>\n\n\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nconst nums = &#x5B;1, 2, 3];\nconst squares = nums.map(n =&gt; n * n);\nconsole.log(squares); \/\/ &#x5B;1, 4, 9]\n\n<\/pre><\/div>\n\n\n<p><strong>\u6ce8\u610f\u4e8b\u9879\uff1a<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u4e0d\u6539\u53d8\u539f\u6570\u7ec4<\/li>\n\n\n\n<li>\u5e38\u7528\u4e8e\u6570\u636e\u683c\u5f0f\u8f6c\u6362<\/li>\n\n\n\n<li>\u56de\u8c03\u8fd4\u56de\u503c\u7ec4\u6210\u65b0\u6570\u7ec4<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\ud83d\udd25 5\ufe0f\u20e3 <code>flatMap<\/code> \u65b9\u6cd5<\/h1>\n\n\n\n<p><strong>\u4f5c\u7528\uff1a<\/strong><br>\u5148\u6267\u884c <code>map<\/code>\uff0c\u7136\u540e <strong>\u6241\u5e73\u5316\u4e00\u5c42<\/strong>\u3002\u5e38\u7528\u4e8e\u5904\u7406\u5d4c\u5957\u6570\u7ec4\u3002<\/p>\n\n\n\n<p><strong>\u8bed\u6cd5\uff1a<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\narr.flatMap(callback(element, index, array))\n\n<\/pre><\/div>\n\n\n<p><strong>\u793a\u4f8b 1\uff1a\u6241\u5e73\u5316\u4e00\u5c42\u6570\u7ec4<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nconst arr = &#x5B;1, 2, 3];\nconst result = arr.flatMap(n =&gt; &#x5B;n, n * 2]);\nconsole.log(result); \/\/ &#x5B;1,2,2,4,3,6]\n\n<\/pre><\/div>\n\n\n<p><strong>\u793a\u4f8b 2\uff1a\u5904\u7406\u5d4c\u5957\u6570\u7ec4<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nconst nested = &#x5B;&#x5B;1,2], &#x5B;3,4]];\nconst flattened = nested.flatMap(x =&gt; x.map(n =&gt; n*2));\nconsole.log(flattened); \/\/ &#x5B;2,4,6,8]\n\n<\/pre><\/div>\n\n\n<p><strong>\u6ce8\u610f\u4e8b\u9879\uff1a<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u53ea\u6241\u5e73\u5316\u4e00\u5c42<\/li>\n\n\n\n<li>\u7b49\u540c\u4e8e <code>arr.map(...).flat(1)<\/code><\/li>\n\n\n\n<li>\u539f\u6570\u7ec4\u4e0d\u53d8<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\ud83d\udd25 6\ufe0f\u20e3 <code>some<\/code> \u65b9\u6cd5<\/h1>\n\n\n\n<p><strong>\u4f5c\u7528\uff1a<\/strong><br>\u68c0\u6d4b\u6570\u7ec4\u4e2d <strong>\u662f\u5426\u81f3\u5c11\u6709\u4e00\u4e2a\u5143\u7d20\u7b26\u5408\u6761\u4ef6<\/strong>\uff0c\u8fd4\u56de\u5e03\u5c14\u503c\u3002<\/p>\n\n\n\n<p><strong>\u8bed\u6cd5\uff1a<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\narr.some(callback(element, index, array))\n\n<\/pre><\/div>\n\n\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nconst numbers = &#x5B;1, 2, 3, 4];\nconst hasEven = numbers.some(n =&gt; n % 2 === 0);\nconsole.log(hasEven); \/\/ true\n\n<\/pre><\/div>\n\n\n<p><strong>\u6ce8\u610f\u4e8b\u9879\uff1a<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u56de\u8c03\u8fd4\u56de <code>true<\/code> \u65f6\u7acb\u5373\u8fd4\u56de <code>true<\/code>\uff0c\u77ed\u8def<\/li>\n\n\n\n<li>\u5e38\u7528\u4e8e\u5224\u65ad\u6761\u4ef6\u662f\u5426\u5b58\u5728<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\ud83d\udd25 7\ufe0f\u20e3 \u5bf9\u6bd4\u603b\u7ed3<\/h1>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u65b9\u6cd5<\/th><th>\u8fd4\u56de\u503c<\/th><th>\u505c\u6b62\u6761\u4ef6<\/th><th>\u539f\u6570\u7ec4\u6539\u53d8<\/th><\/tr><\/thead><tbody><tr><td>find<\/td><td>\u7b2c\u4e00\u4e2a\u7b26\u5408\u6761\u4ef6\u7684\u5143\u7d20<\/td><td>\u627e\u5230\u7b2c\u4e00\u4e2a<\/td><td>\u5426<\/td><\/tr><tr><td>findIndex<\/td><td>\u7b2c\u4e00\u4e2a\u7b26\u5408\u6761\u4ef6\u7684\u7d22\u5f15<\/td><td>\u627e\u5230\u7b2c\u4e00\u4e2a<\/td><td>\u5426<\/td><\/tr><tr><td>filter<\/td><td>\u7b26\u5408\u6761\u4ef6\u7684\u6240\u6709\u5143\u7d20\u6570\u7ec4<\/td><td>\u904d\u5386\u5b8c\u5168\u90e8<\/td><td>\u5426<\/td><\/tr><tr><td>map<\/td><td>\u65b0\u6570\u7ec4\uff0c\u6bcf\u4e2a\u5143\u7d20\u7ecf\u8fc7\u56de\u8c03<\/td><td>\u904d\u5386\u5b8c\u5168\u90e8<\/td><td>\u5426<\/td><\/tr><tr><td>flatMap<\/td><td>\u65b0\u6570\u7ec4\uff08map + \u6241\u5e73\u5316\u4e00\u5c42\uff09<\/td><td>\u904d\u5386\u5b8c\u5168\u90e8<\/td><td>\u5426<\/td><\/tr><tr><td>some<\/td><td>\u5e03\u5c14\u503c<\/td><td>\u627e\u5230\u7b2c\u4e00\u4e2a\u7b26\u5408<\/td><td>\u5426<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\ud83d\udd25 8\ufe0f\u20e3 \u5c0f\u7ed3<\/h1>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u67e5\u627e\u5355\u4e2a\u5143\u7d20<\/strong> \u2192 <code>find<\/code> \/ <code>findIndex<\/code><\/li>\n\n\n\n<li><strong>\u8fc7\u6ee4\u591a\u4e2a\u5143\u7d20<\/strong> \u2192 <code>filter<\/code><\/li>\n\n\n\n<li><strong>\u8f6c\u6362\u6570\u7ec4<\/strong> \u2192 <code>map<\/code> \/ <code>flatMap<\/code><\/li>\n\n\n\n<li><strong>\u5224\u65ad\u5b58\u5728\u6027<\/strong> \u2192 <code>some<\/code><\/li>\n<\/ol>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>Tip\uff1a\u7ed3\u5408 <code>filter<\/code> + <code>map<\/code> \/ <code>flatMap<\/code> \u53ef\u4ee5\u5b9e\u73b0\u590d\u6742\u6570\u7ec4\u64cd\u4f5c<\/p>\n<\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>\u4e0b\u9762\u7ed9\u4f60\u6574\u7406\u4e00\u4efd JavaScript \u6570\u7ec4\u5e38\u7528\u65b9\u6cd5\u8be6\u89e3\uff0c\u5305\u62ec find\u3001fin&#8230; <a class=\"more-link\" href=\"https:\/\/www.52runoob.com\/index.php\/2025\/12\/05\/javascript%e6%95%b0%e7%bb%84%e5%b8%b8%e7%94%a8%e6%96%b9%e6%b3%95find%e3%80%81findindex%e3%80%81filter%e3%80%81map%e3%80%81flatmap%e5%8f%8asome%e8%af%a6%e8%a7%a3\/\">Continue Reading &rarr;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[49],"tags":[],"class_list":["post-532","post","type-post","status-publish","format-standard","hentry","category-javascript"],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts\/532","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/comments?post=532"}],"version-history":[{"count":1,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts\/532\/revisions"}],"predecessor-version":[{"id":533,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts\/532\/revisions\/533"}],"wp:attachment":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/media?parent=532"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/categories?post=532"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/tags?post=532"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}