{"id":3316,"date":"2024-11-10T12:35:54","date_gmt":"2024-11-10T04:35:54","guid":{"rendered":"https:\/\/fwq.ai\/blog\/3316\/"},"modified":"2024-11-10T12:35:54","modified_gmt":"2024-11-10T04:35:54","slug":"js%e5%a6%82%e4%bd%95%e6%8b%bf%e5%88%b0%e6%95%b0%e7%bb%84%e7%b4%a2%e5%bc%95","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/3316\/","title":{"rendered":"js\u5982\u4f55\u62ff\u5230\u6570\u7ec4\u7d22\u5f15"},"content":{"rendered":"<blockquote><p>\n  \u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u65b9\u6cd5\u83b7\u53d6 javascript \u6570\u7ec4\u5143\u7d20\u7684\u7d22\u5f15\uff1a\u4f7f\u7528\u4e0b\u6807\u8fd0\u7b97\u7b26\u76f4\u63a5\u8fd4\u56de\u6307\u5b9a\u7d22\u5f15\u7684\u5143\u7d20\u3002\u4f7f\u7528 indexof() \u65b9\u6cd5\u8fd4\u56de\u5143\u7d20\u7b2c\u4e00\u6b21\u51fa\u73b0\u7684\u7d22\u5f15\uff0c\u5982\u679c\u4e0d\u5b58\u5728\uff0c\u5219\u8fd4\u56de -1\u3002\u4f7f\u7528 lastindexof() \u65b9\u6cd5\u8fd4\u56de\u5143\u7d20\u6700\u540e\u4e00\u6b21\u51fa\u73b0\u7684\u7d22\u5f15\u3002\u4f7f\u7528 findindex() \u65b9\u6cd5\u8fd4\u56de\u7b2c\u4e00\u4e2a\u6ee1\u8db3\u6761\u4ef6\u7684\u5143\u7d20\u7684\u7d22\u5f15\u3002\u4f7f\u7528 findlastindex() \u65b9\u6cd5\u8fd4\u56de\u6700\u540e\u4e00\u4e2a\u6ee1\u8db3\u6761\u4ef6\u7684\u5143\u7d20\u7684\u7d22\u5f15\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202411\/08\/2024110809365733390.jpg\" class=\"aligncenter\" title=\"js\u5982\u4f55\u62ff\u5230\u6570\u7ec4\u7d22\u5f15\u63d2\u56fe\" alt=\"js\u5982\u4f55\u62ff\u5230\u6570\u7ec4\u7d22\u5f15\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u4f7f\u7528 JavaScript \u83b7\u53d6\u6570\u7ec4\u7d22\u5f15<\/strong><\/p>\n<p>\u5728 JavaScript \u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\u83b7\u53d6\u6570\u7ec4\u5143\u7d20\u7684\u7d22\u5f15\uff1a<\/p>\n<p><strong>1. \u4f7f\u7528\u4e0b\u6807\u8fd0\u7b97\u7b26<\/strong><\/p>\n<p>\u6700\u7b80\u5355\u7684\u65b9\u6cd5\u662f\u4f7f\u7528\u4e0b\u6807\u8fd0\u7b97\u7b26\uff08[]\uff09\uff0c\u5b83\u76f4\u63a5\u8fd4\u56de\u6307\u5b9a\u7d22\u5f15\u7684\u5143\u7d20\u3002<\/p>\n<pre>const arr = [1, 2, 3, 4, 5];\nconst index = 2;\nconsole.log(arr[index]); \/\/ \u8f93\u51fa\uff1a3<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>2. \u4f7f\u7528 indexOf() \u65b9\u6cd5<\/strong><\/p>\n<p>indexOf() \u65b9\u6cd5\u8fd4\u56de\u6307\u5b9a\u5143\u7d20\u5728\u6570\u7ec4\u4e2d\u7b2c\u4e00\u6b21\u51fa\u73b0\u65f6\u7684\u7d22\u5f15\uff0c\u5982\u679c\u4e0d\u5b58\u5728\uff0c\u5219\u8fd4\u56de -1\u3002<\/p>\n<pre>const arr = [1, 2, 3, 4, 5];\nconst element = 3;\nconsole.log(arr.indexOf(element)); \/\/ \u8f93\u51fa\uff1a2<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>3. \u4f7f\u7528 lastIndexOf() \u65b9\u6cd5<\/strong><\/p>\n<p>lastIndexOf() \u65b9\u6cd5\u4e0e indexOf() \u7c7b\u4f3c\uff0c\u4f46\u5b83\u8fd4\u56de\u6307\u5b9a\u5143\u7d20\u5728\u6570\u7ec4\u4e2d\u6700\u540e\u4e00\u6b21\u51fa\u73b0\u65f6\u7684\u7d22\u5f15\u3002<\/p>\n<pre>const arr = [1, 2, 3, 4, 5, 3];\nconst element = 3;\nconsole.log(arr.lastIndexOf(element)); \/\/ \u8f93\u51fa\uff1a5<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>4. \u4f7f\u7528 findIndex() \u65b9\u6cd5<\/strong><\/p>\n<p>findIndex() \u65b9\u6cd5\u8fd4\u56de\u7b2c\u4e00\u4e2a\u6ee1\u8db3\u7ed9\u5b9a\u6761\u4ef6\u7684\u5143\u7d20\u7684\u7d22\u5f15\u3002\u6761\u4ef6\u7531\u4e00\u4e2a\u56de\u8c03\u51fd\u6570\u6307\u5b9a\uff0c\u8be5\u51fd\u6570\u63a5\u6536\u6570\u7ec4\u5143\u7d20\u3001\u7d22\u5f15\u548c\u6570\u7ec4\u672c\u8eab\u4f5c\u4e3a\u53c2\u6570\u3002<\/p>\n<pre>const arr = [1, 2, 3, 4, 5];\nconst index = arr.findIndex(element =&gt; element &gt;= 3);\nconsole.log(index); \/\/ \u8f93\u51fa\uff1a2<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>5. \u4f7f\u7528 findLastIndex() \u65b9\u6cd5<\/strong><\/p>\n<p>findLastIndex() \u65b9\u6cd5\u4e0e findIndex() \u7c7b\u4f3c\uff0c\u4f46\u5b83\u8fd4\u56de\u6700\u540e\u4e00\u4e2a\u6ee1\u8db3\u7ed9\u5b9a\u6761\u4ef6\u7684\u5143\u7d20\u7684\u7d22\u5f15\u3002<\/p>\n<pre>const arr = [1, 2, 3, 4, 5, 3];\nconst index = arr.findLastIndex(element =&gt; element &gt;= 3);\nconsole.log(index); \/\/ \u8f93\u51fa\uff1a5<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fjs\u5982\u4f55\u62ff\u5230\u6570\u7ec4\u7d22\u5f15\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>\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u65b9\u6cd5\u83b7\u53d6 javascript \u6570\u7ec4\u5143\u7d20\u7684\u7d22\u5f15\uff1a\u4f7f\u7528\u4e0b\u6807\u8fd0\u7b97\u7b26\u76f4\u63a5\u8fd4\u56de\u6307\u5b9a\u7d22\u5f15\u7684\u5143\u7d20\u3002\u4f7f\u7528 indexof() \u65b9\u6cd5\u8fd4\u56de\u5143\u7d20\u7b2c\u4e00\u6b21\u51fa\u73b0\u7684\u7d22\u5f15\uff0c\u5982\u679c\u4e0d\u5b58\u5728\uff0c\u5219\u8fd4\u56de -1\u3002\u4f7f\u7528 lastindexof() \u65b9\u6cd5\u8fd4\u56de\u5143\u7d20\u6700\u540e\u4e00\u6b21\u51fa\u73b0\u7684\u7d22\u5f15\u3002\u4f7f\u7528 findindex() \u65b9\u6cd5\u8fd4\u56de\u7b2c\u4e00\u4e2a\u6ee1\u8db3\u6761\u4ef6\u7684\u5143\u7d20\u7684\u7d22\u5f15\u3002\u4f7f\u7528 findlastindex() \u65b9\u6cd5\u8fd4\u56de\u6700\u540e\u4e00\u4e2a\u6ee1\u8db3\u6761\u4ef6\u7684\u5143\u7d20\u7684\u7d22\u5f15\u3002 \u5982\u4f55\u4f7f\u7528 JavaScript \u83b7\u53d6\u6570\u7ec4\u7d22\u5f15 \u5728 JavaScript \u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\u83b7\u53d6\u6570\u7ec4\u5143\u7d20\u7684\u7d22\u5f15\uff1a 1. \u4f7f\u7528\u4e0b\u6807\u8fd0\u7b97\u7b26 \u6700\u7b80\u5355\u7684\u65b9\u6cd5\u662f\u4f7f\u7528\u4e0b\u6807\u8fd0\u7b97\u7b26\uff08[]\uff09\uff0c\u5b83\u76f4\u63a5\u8fd4\u56de\u6307\u5b9a\u7d22\u5f15\u7684\u5143\u7d20\u3002 const arr = [1, 2, 3, 4, 5]; const index = 2; console.log(arr[index]); \/\/ \u8f93\u51fa\uff1a3 \u767b\u5f55\u540e\u590d\u5236 2. \u4f7f\u7528 indexOf() \u65b9\u6cd5 indexOf() \u65b9\u6cd5\u8fd4\u56de\u6307\u5b9a\u5143\u7d20\u5728\u6570\u7ec4\u4e2d\u7b2c\u4e00\u6b21\u51fa\u73b0\u65f6\u7684\u7d22\u5f15\uff0c\u5982\u679c\u4e0d\u5b58\u5728\uff0c\u5219\u8fd4\u56de -1\u3002 const arr = [1, 2, 3, 4, 5]; const element = [&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-3316","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/3316","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=3316"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/3316\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=3316"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=3316"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=3316"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}