{"id":964,"date":"2025-11-04T15:27:07","date_gmt":"2025-11-04T07:27:07","guid":{"rendered":"https:\/\/www.ikaros.love\/?p=964"},"modified":"2025-11-04T15:27:07","modified_gmt":"2025-11-04T07:27:07","slug":"javascript-%e8%b0%83%e8%af%95%e9%99%b7%e9%98%b1%ef%bc%9a-indexof-%e5%92%8c-console-log-%e4%bc%98%e5%85%88%e7%ba%a7%e5%8f%a0%e5%8a%a0","status":"publish","type":"post","link":"https:\/\/www.ikaros.love\/?p=964","title":{"rendered":"JavaScript \u8c03\u8bd5\u9677\u9631\uff1a indexOf \u548c console.log \u4f18\u5148\u7ea7\u53e0\u52a0"},"content":{"rendered":"\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"556\" height=\"540\" src=\"https:\/\/www.ikaros.love\/wp-content\/uploads\/2025\/11\/86.png\" alt=\"\" class=\"wp-image-965\" srcset=\"https:\/\/www.ikaros.love\/wp-content\/uploads\/2025\/11\/86.png 556w, https:\/\/www.ikaros.love\/wp-content\/uploads\/2025\/11\/86-300x291.png 300w\" sizes=\"(max-width: 556px) 100vw, 556px\" \/><\/figure>\n\n\n\n<p>\u6700\u8fd1\u5728\u5904\u7406\u4e00\u4e2a\u770b\u4f3c\u7b80\u5355\u7684\u6570\u7ec4\u5220\u9664\u64cd\u4f5c\u65f6\uff0c\u9047\u5230\u4e86\u4e00\u4e2a\u975e\u5e38\u8ba9\u4eba\u56f0\u60d1\u7684 Bug\u3002\u6211\u7684\u76ee\u6807\u662f\u6839\u636e\u5bf9\u8c61\u7684 <code>caseId<\/code> \u4ece\u6570\u7ec4 <code>allCaseDataList<\/code> \u4e2d\u79fb\u9664\u4e00\u4e2a\u5143\u7d20\u3002<\/p>\n\n\n\n<p>\u6211\u7684\u539f\u59cb\u4ee3\u7801\u7247\u6bb5\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ \u67e5\u627e\u7d22\u5f15<br>const index = this.allCaseDataList.indexOf(item =&gt; (item.caseId.toString() === caseId.toString()));<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ \u8c03\u8bd5\u8f93\u51fa\nfor(let i=0; i&lt;this.allCaseDataList.length; i++){\nconsole.log(\"\u5bf9\u6bd4\u7ed3\u679c: \"+this.allCaseDataList&#91;i].caseId.toString() == caseId.toString());\n}\n<\/code><\/pre>\n\n\n\n<p>\u63a7\u5236\u53f0\u7684\u8f93\u51fa\u8ba9\u6211\u9677\u5165\u4e86\u5de8\u5927\u7684\u56f0\u60d1\uff1a<code>\u5220\u9664index<\/code> \u603b\u662f <code>-1<\/code>\uff0c\u800c\u6211\u5728\u5faa\u73af\u4e2d\u660e\u660e\u770b\u5230\u4e24\u4e2a <code>caseId<\/code> \u6253\u5370\u51fa\u6765\u662f\u76f8\u7b49\u7684\uff0c\u4f46 <code>\u5bf9\u6bd4\u7ed3\u679c<\/code> \u5374\u663e\u793a <code>false<\/code>\u3002<\/p>\n\n\n\n<p>\u7ecf\u8fc7\u6392\u67e5\uff0c\u53d1\u73b0\u8fd9\u91cc\u53e0\u52a0\u4e86\u4e24\u4e2a\u975e\u5e38\u7ecf\u5178\u7684 JavaScript \u9677\u9631\uff1a<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u9677\u9631\u4e00\uff1a<code>indexOf<\/code> \u7684\u9519\u8bef\u7528\u6cd5<\/h4>\n\n\n\n<p><strong>\u95ee\u9898\uff1a<\/strong> <code>Array.prototype.indexOf()<\/code> \u65e0\u6cd5\u63a5\u53d7\u56de\u8c03\u51fd\u6570\u4f5c\u4e3a\u53c2\u6570\u6765\u8fdb\u884c\u590d\u6742\u67e5\u627e\u3002\u5b83\u53ea\u80fd\u67e5\u627e\u6570\u7ec4\u4e2d<strong>\u5b8c\u5168\u76f8\u7b49<\/strong>\u7684\u5143\u7d20\u3002 <strong>\u540e\u679c\uff1a<\/strong> \u7531\u4e8e\u6211\u4f20\u5165\u4e86\u4e00\u4e2a\u51fd\u6570\u4f5c\u4e3a\u53c2\u6570\uff0c<code>indexOf<\/code> \u627e\u4e0d\u5230\u8fd9\u4e2a\u51fd\u6570\u5728\u6570\u7ec4\u4e2d\u7684\u4f4d\u7f6e\uff0c\u6240\u4ee5\u603b\u662f\u8fd4\u56de <code>-1<\/code>\uff0c\u5bfc\u81f4\u6838\u5fc3\u5220\u9664\u903b\u8f91\u5931\u8d25\u3002 <strong>\u89e3\u51b3\u65b9\u6848\uff1a<\/strong> \u5e94\u8be5\u4f7f\u7528 <code>Array.prototype.findIndex()<\/code> \u6765\u5b9e\u73b0\u57fa\u4e8e\u5c5e\u6027\u7684\u6761\u4ef6\u67e5\u627e\uff1a<\/p>\n\n\n\n<p>JavaScript<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const index = this.allCaseDataList.findIndex(item =&gt; (item.caseId.toString() === caseId.toString()));\n<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">\u9677\u9631\u4e8c\uff1a<code>console.log<\/code> \u4e2d\u7684\u8fd0\u7b97\u7b26\u4f18\u5148\u7ea7<\/h4>\n\n\n\n<p><strong>\u95ee\u9898\uff1a<\/strong> \u4e3a\u4ec0\u4e48\u6211\u5728\u63a7\u5236\u53f0\u770b\u5230\u7684\u4e24\u4e2a <code>caseId<\/code> \u76f8\u7b49\uff0c\u4f46\u6bd4\u8f83\u7ed3\u679c\u5374\u662f <code>false<\/code>\uff1f\u8fd9\u662f\u56e0\u4e3a\u5728 <code>console.log<\/code> \u8bed\u53e5\u4e2d\uff0c<strong>\u5b57\u7b26\u4e32\u62fc\u63a5\u64cd\u4f5c\u7b26 <code>+<\/code> \u7684\u4f18\u5148\u7ea7\u9ad8\u4e8e\u76f8\u7b49\u6bd4\u8f83\u64cd\u4f5c\u7b26 <code>==<\/code><\/strong>\u3002<\/p>\n\n\n\n<p>\u6211\u7684\u4ee3\u7801\uff1a <code>console.log(\"\u5bf9\u6bd4\u7ed3\u679c: \"+this.allCaseDataList[i].caseId.toString() == caseId.toString());<\/code><\/p>\n\n\n\n<p>JavaScript \u4f1a\u5148\u6267\u884c\u5de6\u4fa7\u7684\u62fc\u63a5\uff0c\u751f\u6210\u4e00\u4e2a\u8d85\u957f\u7684\u5b57\u7b26\u4e32\uff0c\u4f8b\u5982 <code>\"\u5bf9\u6bd4\u7ed3\u679c: 1971113746458480640\"<\/code>\u3002\u7136\u540e\u518d\u7528\u8fd9\u4e2a\u957f\u5b57\u7b26\u4e32\u53bb\u548c\u53f3\u4fa7\u7684 <code>caseId<\/code> \u5b57\u7b26\u4e32\u8fdb\u884c\u6bd4\u8f83\uff0c\u7ed3\u679c\u81ea\u7136\u662f <code>false<\/code>\u3002<\/p>\n\n\n\n<p><strong>\u89e3\u51b3\u65b9\u6848\uff1a<\/strong> \u5fc5\u987b\u4f7f\u7528\u62ec\u53f7\u6765\u5f3a\u5236\u5148\u6267\u884c\u6bd4\u8f83\u64cd\u4f5c\uff1a<\/p>\n\n\n\n<p>JavaScript<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>console.log(\"\u5bf9\u6bd4\u7ed3\u679c: \" + (this.allCaseDataList&#91;i].caseId.toString() == caseId.toString()));\n<\/code><\/pre>\n\n\n\n<p><strong>\u603b\u7ed3\uff1a<\/strong> \u5728\u8fd9\u4e2a\u6848\u4f8b\u4e2d\uff0c<code>indexOf<\/code> \u7684\u9519\u8bef\u7528\u6cd5\u5bfc\u81f4\u4e86\u529f\u80fd\u5931\u8d25\uff0c\u800c <code>console.log<\/code> \u7684\u4f18\u5148\u7ea7\u95ee\u9898\u5219\u4e25\u91cd\u5e72\u6270\u4e86\u6211\u7684\u8c03\u8bd5\u5224\u65ad\u3002\u8fd9\u4e24\u4e2a\u9519\u8bef\u7684\u53e0\u52a0\uff0c\u8ba9\u539f\u672c\u7b80\u5355\u7684\u95ee\u9898\u53d8\u5f97\u5f02\u5e38\u590d\u6742\u3002\u901a\u8fc7\u8fd9\u6b21\u7ecf\u5386\uff0c\u518d\u6b21\u63d0\u9192\u6211\u4eec\uff0c\u5728\u8fdb\u884c JavaScript \u8c03\u8bd5\u65f6\uff0c\u4e00\u5b9a\u8981\u8b66\u60d5\u8fd0\u7b97\u7b26\u4f18\u5148\u7ea7\u548c\u539f\u751f\u65b9\u6cd5\u7684\u6b63\u786e\u7528\u6cd5\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6700\u8fd1\u5728\u5904\u7406\u4e00\u4e2a\u770b\u4f3c\u7b80\u5355\u7684\u6570\u7ec4\u5220\u9664&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":965,"comment_status":"open","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[259,258,260,261],"_links":{"self":[{"href":"https:\/\/www.ikaros.love\/index.php?rest_route=\/wp\/v2\/posts\/964"}],"collection":[{"href":"https:\/\/www.ikaros.love\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.ikaros.love\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.ikaros.love\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.ikaros.love\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=964"}],"version-history":[{"count":1,"href":"https:\/\/www.ikaros.love\/index.php?rest_route=\/wp\/v2\/posts\/964\/revisions"}],"predecessor-version":[{"id":966,"href":"https:\/\/www.ikaros.love\/index.php?rest_route=\/wp\/v2\/posts\/964\/revisions\/966"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.ikaros.love\/index.php?rest_route=\/wp\/v2\/media\/965"}],"wp:attachment":[{"href":"https:\/\/www.ikaros.love\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=964"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.ikaros.love\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=964"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.ikaros.love\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=964"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}