{"id":2084,"date":"2024-11-10T17:06:34","date_gmt":"2024-11-10T09:06:34","guid":{"rendered":"https:\/\/fwq.ai\/blog\/2084\/"},"modified":"2024-11-10T17:06:34","modified_gmt":"2024-11-10T09:06:34","slug":"%e4%b8%ba%e4%bb%80%e4%b9%88-filter-%e6%96%b9%e6%b3%95%e5%8f%aa%e8%bf%94%e5%9b%9e%e4%b8%80%e4%b8%aa-a%ef%bc%8c%e8%80%8c%e4%b8%8d%e6%98%af%e4%b8%a4%e4%b8%aa%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/2084\/","title":{"rendered":"\u4e3a\u4ec0\u4e48 filter() \u65b9\u6cd5\u53ea\u8fd4\u56de\u4e00\u4e2a &#8220;a&#8221;\uff0c\u800c\u4e0d\u662f\u4e24\u4e2a\uff1f"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173063899829814.jpg\" class=\"aligncenter\" title=\"\u4e3a\u4ec0\u4e48 filter() \u65b9\u6cd5\u53ea\u8fd4\u56de\u4e00\u4e2a &#8220;a&#8221;\uff0c\u800c\u4e0d\u662f\u4e24\u4e2a\uff1f\u63d2\u56fe\" alt=\"\u4e3a\u4ec0\u4e48 filter() \u65b9\u6cd5\u53ea\u8fd4\u56de\u4e00\u4e2a &#8220;a&#8221;\uff0c\u800c\u4e0d\u662f\u4e24\u4e2a\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>filter() \u65b9\u6cd5\u7684\u8fd4\u56de\u503c\u89e3\u6790<\/strong><\/p>\n<p>javascript \u4e2d\u7684 filter() \u65b9\u6cd5\u7528\u4e8e\u8fc7\u6ee4\u6570\u7ec4\u4e2d\u7b26\u5408\u7279\u5b9a\u6761\u4ef6\u7684\u5143\u7d20\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2a\u5305\u542b\u8fd9\u4e9b\u5143\u7d20\u7684\u65b0\u6570\u7ec4\u3002<\/p>\n<p><strong>\u95ee\u9898\uff1afilter() \u8fd4\u56de\u503c\u7684\u8ff7\u60d1<\/strong><\/p>\n<p>\u4ee3\u7801\u793a\u4f8b\uff1a<\/p>\n<pre>const arr = [\"a\", \"b\", \"c\", \"a\"];\nconst result = arr.filter(item =&gt; item.indexof(\"a\") === 0);\nconsole.log(result); \/\/ \u8f93\u51fa\uff1a[\"a\"]<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee3\u7801\u4e2d\uff0cfilter() \u65b9\u6cd5\u4f7f\u7528\u4e86\u4e00\u4e2a\u7bad\u5934\u51fd\u6570\uff0c\u8be5\u51fd\u6570\u68c0\u67e5\u6570\u7ec4\u4e2d\u6bcf\u4e2a\u5143\u7d20\u662f\u5426\u4ee5 &#8220;a&#8221; \u5f00\u5934\u3002\u7406\u8bba\u4e0a\uff0c\u6ee1\u8db3\u6761\u4ef6\u7684\u5e94\u8be5\u662f &#8220;a&#8221; \u548c &#8220;a&#8221;\uff0c\u4f46\u8f93\u51fa\u7ed3\u679c\u5374\u53ea\u5305\u542b &#8220;a&#8221;\u3002\u8fd9\u8ba9\u4eba\u7591\u60d1\u4e0d\u89e3\u3002<\/p>\n<p><strong>\u89e3\u7b54\uff1aindexof() \u7684\u7ec6\u5fae\u5dee\u522b<\/strong><\/p>\n<p>indexof() \u65b9\u6cd5\u7528\u4e8e\u67e5\u627e\u5b57\u7b26\u4e32\u4e2d\u6307\u5b9a\u5b50\u4e32\u7684\u9996\u6b21\u51fa\u73b0\u4f4d\u7f6e\u3002\u5982\u679c\u627e\u4e0d\u5230\u8be5\u5b50\u4e32\uff0c\u5b83\u5c06\u8fd4\u56de -1\u3002\u5728\u672c\u6587\u7684\u4f8b\u5b50\u91cc\uff0c\u56e0\u4e3a &#8220;b&#8221; \u548c &#8220;c&#8221; \u4e2d\u4e0d\u5305\u542b &#8220;a&#8221;\uff0c\u56e0\u6b64 item.indexof(&#8220;a&#8221;) \u7b49\u4e8e -1\u3002<\/p>\n<p><strong>number \u8f6c boolean<\/strong><\/p>\n<p>\u5728 javascript \u4e2d\uff0c\u6570\u5b57\u7c7b\u578b\u7684\u503c\u53ef\u4ee5\u4e3a boolean \u7c7b\u578b\u7684\u503c\u3002\u9664\u4e86 0 \u548c nan \u4e4b\u5916\u7684\u4efb\u4f55\u6570\u5b57\u503c\u90fd\u4f1a\u88ab\u8f6c\u6362\u4e3a true\u3002\u56e0\u6b64\uff0c\u867d\u7136 -1 \u5728\u6570\u5b66\u4e0a\u4e3a\u8d1f\u6570\uff0c\u4f46\u5728 javascript \u4e2d\u5b83\u4f1a\u88ab\u8f6c\u6362\u4e3a true\u3002<\/p>\n<p><strong>\u6700\u7ec8\u7ed3\u679c<\/strong><\/p>\n<p>\u5728\u63d0\u4f9b\u7684\u4ee3\u7801\u793a\u4f8b\u4e2d\uff0c\u7bad\u5934\u51fd\u6570\u5b9e\u9645\u4e0a\u8fd4\u56de\u7684\u662f\uff1a<\/p>\n<pre>return item.indexOf(\"a\") === 0 ? true : false;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u7531\u4e8e &#8220;b&#8221; \u548c &#8220;c&#8221; \u6ee1\u8db3\u6b64\u6761\u4ef6\uff0c\u56e0\u6b64\u6700\u7ec8\u7ed3\u679c\u53ea\u5305\u542b &#8220;a&#8221;\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u4e3a\u4ec0\u4e48 filter() \u65b9\u6cd5\u53ea\u8fd4\u56de\u4e00\u4e2a &#8220;a&#8221;\uff0c\u800c\u4e0d\u662f\u4e24\u4e2a\uff1f\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>filter() \u65b9\u6cd5\u7684\u8fd4\u56de\u503c\u89e3\u6790 javascript \u4e2d\u7684 filter() \u65b9\u6cd5\u7528\u4e8e\u8fc7\u6ee4\u6570\u7ec4\u4e2d\u7b26\u5408\u7279\u5b9a\u6761\u4ef6\u7684\u5143\u7d20\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2a\u5305\u542b\u8fd9\u4e9b\u5143\u7d20\u7684\u65b0\u6570\u7ec4\u3002 \u95ee\u9898\uff1afilter() \u8fd4\u56de\u503c\u7684\u8ff7\u60d1 \u4ee3\u7801\u793a\u4f8b\uff1a const arr = [&#8220;a&#8221;, &#8220;b&#8221;, &#8220;c&#8221;, &#8220;a&#8221;]; const result = arr.filter(item =&gt; item.indexof(&#8220;a&#8221;) === 0); console.log(result); \/\/ \u8f93\u51fa\uff1a[&#8220;a&#8221;] \u767b\u5f55\u540e\u590d\u5236 \u4ee3\u7801\u4e2d\uff0cfilter() \u65b9\u6cd5\u4f7f\u7528\u4e86\u4e00\u4e2a\u7bad\u5934\u51fd\u6570\uff0c\u8be5\u51fd\u6570\u68c0\u67e5\u6570\u7ec4\u4e2d\u6bcf\u4e2a\u5143\u7d20\u662f\u5426\u4ee5 &#8220;a&#8221; \u5f00\u5934\u3002\u7406\u8bba\u4e0a\uff0c\u6ee1\u8db3\u6761\u4ef6\u7684\u5e94\u8be5\u662f &#8220;a&#8221; \u548c &#8220;a&#8221;\uff0c\u4f46\u8f93\u51fa\u7ed3\u679c\u5374\u53ea\u5305\u542b &#8220;a&#8221;\u3002\u8fd9\u8ba9\u4eba\u7591\u60d1\u4e0d\u89e3\u3002 \u89e3\u7b54\uff1aindexof() \u7684\u7ec6\u5fae\u5dee\u522b indexof() \u65b9\u6cd5\u7528\u4e8e\u67e5\u627e\u5b57\u7b26\u4e32\u4e2d\u6307\u5b9a\u5b50\u4e32\u7684\u9996\u6b21\u51fa\u73b0\u4f4d\u7f6e\u3002\u5982\u679c\u627e\u4e0d\u5230\u8be5\u5b50\u4e32\uff0c\u5b83\u5c06\u8fd4\u56de -1\u3002\u5728\u672c\u6587\u7684\u4f8b\u5b50\u91cc\uff0c\u56e0\u4e3a &#8220;b&#8221; \u548c &#8220;c&#8221; \u4e2d\u4e0d\u5305\u542b &#8220;a&#8221;\uff0c\u56e0\u6b64 item.indexof(&#8220;a&#8221;) \u7b49\u4e8e -1\u3002 number \u8f6c boolean \u5728 javascript \u4e2d\uff0c\u6570\u5b57\u7c7b\u578b\u7684\u503c\u53ef\u4ee5\u4e3a [&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-2084","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/2084","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=2084"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/2084\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=2084"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=2084"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=2084"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}