René's URL Explorer Experiment


Title: 编程题:用最简洁代码实现 indexOf 方法 · Issue #58 · sisterAn/JavaScript-Algorithms · GitHub

Open Graph Title: 编程题:用最简洁代码实现 indexOf 方法 · Issue #58 · sisterAn/JavaScript-Algorithms

X Title: 编程题:用最简洁代码实现 indexOf 方法 · Issue #58 · sisterAn/JavaScript-Algorithms

Description: indexOf 有两种: String.prototype.indexOf() 返回从 fromIndex 处开始搜索第一次出现的指定值的索引,如果未找到,返回 -1 str.indexOf(searchValue [, fromIndex]) // fromIndex 默认值为 0 Array.prototype.indexOf() 返回在数组中可以找到一个给定元素的第一个索引,如果不存在,则返回 -1 arr.indexOf(searchElement[, from...

Open Graph Description: indexOf 有两种: String.prototype.indexOf() 返回从 fromIndex 处开始搜索第一次出现的指定值的索引,如果未找到,返回 -1 str.indexOf(searchValue [, fromIndex]) // fromIndex 默认值为 0 Array.prototype.indexOf() 返回在数组中可以找到一个给定元素的第一个索引,如果不存在...

X Description: indexOf 有两种: String.prototype.indexOf() 返回从 fromIndex 处开始搜索第一次出现的指定值的索引,如果未找到,返回 -1 str.indexOf(searchValue [, fromIndex]) // fromIndex 默认值为 0 Array.prototype.indexOf() 返回在数组中可以找到一个给定元素的第一个索引,如果不存在...

Opengraph URL: https://github.com/sisterAn/JavaScript-Algorithms/issues/58

X: @github

direct link

Domain: github.com


Hey, it has json ld scripts:
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"编程题:用最简洁代码实现 indexOf 方法","articleBody":"`indexOf` 有两种:\r\n\r\n\u003e String.prototype.indexOf() \r\n\r\n返回从 `fromIndex` 处开始搜索第一次出现的指定值的索引,如果未找到,返回 `-1`\r\n\r\n```js\r\nstr.indexOf(searchValue [, fromIndex])\r\n// fromIndex 默认值为 0\r\n```\r\n\r\n\r\n\r\n\u003e Array.prototype.indexOf()\r\n\r\n返回在数组中可以找到一个给定元素的第一个索引,如果不存在,则返回 `-1`\r\n\r\n```js\r\narr.indexOf(searchElement[, fromIndex])\r\n```\r\n\r\n#### 解答\r\n\r\n##### String.prototype.indexOf() \r\n\r\n**解题思路:正则,字符串匹配**\r\n\r\n```js\r\nfunction sIndexOf(str, searchStr, fromIndex = 0){\r\n    var regex = new RegExp(`${searchStr}`, 'ig')\r\n    regex.lastIndex = fromIndex\r\n    var result = regex.exec(str)\r\n    return result ? result.index : -1\r\n}\r\n\r\n// 测试\r\nvar paragraph = 'The quick brown fox jumps over the lazy dog. If the dog barked, was it really lazy?'\r\nvar searchTerm = 'dog'\r\n// 测试一:不设置 fromIndex\r\nconsole.log(sIndexOf(paragraph, searchTerm))\r\n// 40\r\nconsole.log(paragraph.indexOf(searchTerm));\r\n// 40\r\n// 测试二:设置 fromIndex\r\nconsole.log(sIndexOf(paragraph, searchTerm, 41))\r\n// 52\r\nconsole.log(paragraph.indexOf(searchTerm, 41));\r\n// 52\r\n```\r\n\r\n测试成功\r\n\r\n##### Array.prototype.indexOf()\r\n\r\n**解题思路:遍历匹配**\r\n\r\n```js\r\nfunction aIndexOf(arr, elem, fromIndex = 0){\r\n    if(!elem) return -1\r\n    for(let i = fromIndex; i \u003c arr.length; i++) {\r\n        if(arr[i] === elem) return i\r\n    }\r\n    return -1\r\n}\r\n\r\n// 测试\r\nvar beasts = ['ant', 'bison', 'camel', 'duck', 'bison']\r\n// 测试一:不设置 fromIndex\r\nconsole.log(aIndexOf(beasts, 'bison'))\r\n// 1\r\nconsole.log(beasts.indexOf('bison'))\r\n// 1\r\n// 测试二:设置 fromIndex\r\nconsole.log(aIndexOf(beasts, 'bison', 2))\r\n// 4\r\nconsole.log(beasts.indexOf('bison', 2))\r\n// 4\r\n```\r\n\r\n测试成功\r\n\r\n##### 总结一下\r\n\r\n```js\r\nfunction indexOf(items, item, fromIndex = 0) {\r\n    let isArray = Array.isArray(items);\r\n    let isString = Object.prototype.toString.call(items) == '[object String]';\r\n    if (!isArray \u0026\u0026 !isString) throw new SyntaxError();\r\n    if(isArray) return sIndexOf(items, item, fromIndex)\r\n    else return aIndexOf(items, item, fromIndex)\r\n}\r\n```\r\n\r\n你也可以尝试使用遍历匹配法解决 `sIndexOf` 问题(正则更简洁),这里不做介绍(和 `aIndexOf ` 差不多的套路,不同的是,`String` 类型可以一次匹配多个字符)","author":{"url":"https://github.com/sisterAn","@type":"Person","name":"sisterAn"},"datePublished":"2020-06-02T00:17:53.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":8},"url":"https://github.com/58/JavaScript-Algorithms/issues/58"}

route-pattern/_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format)
route-controllervoltron_issues_fragments
route-actionissue_layout
fetch-noncev2:b95ded1e-a5a9-fead-88a2-5e9b4f9f4130
current-catalog-service-hash81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114
request-idD9C4:22DEEF:13962A5:1AF22B7:696AA02C
html-safe-nonceacfc14dca413e215fcb3a53bfa0992472739bb458d0dd544278ce284111d5eb3
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJEOUM0OjIyREVFRjoxMzk2MkE1OjFBRjIyQjc6Njk2QUEwMkMiLCJ2aXNpdG9yX2lkIjoiNTc2NDM3MTk3MTYxNjQ0ODU1NyIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9
visitor-hmac1c319965e89323af1e815759dd63f967ac41b1ad65a0568e596cbd4eb3bf63d8
hovercard-subject-tagissue:628809975
github-keyboard-shortcutsrepository,issues,copilot
google-site-verificationApib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I
octolytics-urlhttps://collector.github.com/github/collect
analytics-location///voltron/issues_fragments/issue_layout
fb:app_id1401488693436528
apple-itunes-appapp-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/sisterAn/JavaScript-Algorithms/58/issue_layout
twitter:imagehttps://opengraph.githubassets.com/f1815111f22ac3cc499ec6a181feb8ef37409b97d208fdf9d87701cdcce95bda/sisterAn/JavaScript-Algorithms/issues/58
twitter:cardsummary_large_image
og:imagehttps://opengraph.githubassets.com/f1815111f22ac3cc499ec6a181feb8ef37409b97d208fdf9d87701cdcce95bda/sisterAn/JavaScript-Algorithms/issues/58
og:image:altindexOf 有两种: String.prototype.indexOf() 返回从 fromIndex 处开始搜索第一次出现的指定值的索引,如果未找到,返回 -1 str.indexOf(searchValue [, fromIndex]) // fromIndex 默认值为 0 Array.prototype.indexOf() 返回在数组中可以找到一个给定元素的第一个索引,如果不存在...
og:image:width1200
og:image:height600
og:site_nameGitHub
og:typeobject
og:author:usernamesisterAn
hostnamegithub.com
expected-hostnamegithub.com
Nonea51f97dbb9326f71c08ecb61577457d543c602124d1a2672871258ef37ac5261
turbo-cache-controlno-preview
go-importgithub.com/sisterAn/JavaScript-Algorithms git https://github.com/sisterAn/JavaScript-Algorithms.git
octolytics-dimension-user_id19721451
octolytics-dimension-user_loginsisterAn
octolytics-dimension-repository_id252061924
octolytics-dimension-repository_nwosisterAn/JavaScript-Algorithms
octolytics-dimension-repository_publictrue
octolytics-dimension-repository_is_forkfalse
octolytics-dimension-repository_network_root_id252061924
octolytics-dimension-repository_network_root_nwosisterAn/JavaScript-Algorithms
turbo-body-classeslogged-out env-production page-responsive
disable-turbofalse
browser-stats-urlhttps://api.github.com/_private/browser/stats
browser-errors-urlhttps://api.github.com/_private/browser/errors
release4bd0eac606c70914085176ef312ebdcd97a8cdf1
ui-targetfull
theme-color#1e2327
color-schemelight dark

Links:

Skip to contenthttps://github.com/sisterAn/JavaScript-Algorithms/issues/58#start-of-content
https://github.com/
Sign in https://github.com/login?return_to=https%3A%2F%2Fgithub.com%2FsisterAn%2FJavaScript-Algorithms%2Fissues%2F58
GitHub CopilotWrite better code with AIhttps://github.com/features/copilot
GitHub SparkBuild and deploy intelligent appshttps://github.com/features/spark
GitHub ModelsManage and compare promptshttps://github.com/features/models
MCP RegistryNewIntegrate external toolshttps://github.com/mcp
ActionsAutomate any workflowhttps://github.com/features/actions
CodespacesInstant dev environmentshttps://github.com/features/codespaces
IssuesPlan and track workhttps://github.com/features/issues
Code ReviewManage code changeshttps://github.com/features/code-review
GitHub Advanced SecurityFind and fix vulnerabilitieshttps://github.com/security/advanced-security
Code securitySecure your code as you buildhttps://github.com/security/advanced-security/code-security
Secret protectionStop leaks before they starthttps://github.com/security/advanced-security/secret-protection
Why GitHubhttps://github.com/why-github
Documentationhttps://docs.github.com
Bloghttps://github.blog
Changeloghttps://github.blog/changelog
Marketplacehttps://github.com/marketplace
View all featureshttps://github.com/features
Enterpriseshttps://github.com/enterprise
Small and medium teamshttps://github.com/team
Startupshttps://github.com/enterprise/startups
Nonprofitshttps://github.com/solutions/industry/nonprofits
App Modernizationhttps://github.com/solutions/use-case/app-modernization
DevSecOpshttps://github.com/solutions/use-case/devsecops
DevOpshttps://github.com/solutions/use-case/devops
CI/CDhttps://github.com/solutions/use-case/ci-cd
View all use caseshttps://github.com/solutions/use-case
Healthcarehttps://github.com/solutions/industry/healthcare
Financial serviceshttps://github.com/solutions/industry/financial-services
Manufacturinghttps://github.com/solutions/industry/manufacturing
Governmenthttps://github.com/solutions/industry/government
View all industrieshttps://github.com/solutions/industry
View all solutionshttps://github.com/solutions
AIhttps://github.com/resources/articles?topic=ai
Software Developmenthttps://github.com/resources/articles?topic=software-development
DevOpshttps://github.com/resources/articles?topic=devops
Securityhttps://github.com/resources/articles?topic=security
View all topicshttps://github.com/resources/articles
Customer storieshttps://github.com/customer-stories
Events & webinarshttps://github.com/resources/events
Ebooks & reportshttps://github.com/resources/whitepapers
Business insightshttps://github.com/solutions/executive-insights
GitHub Skillshttps://skills.github.com
Documentationhttps://docs.github.com
Customer supporthttps://support.github.com
Community forumhttps://github.com/orgs/community/discussions
Trust centerhttps://github.com/trust-center
Partnershttps://github.com/partners
GitHub SponsorsFund open source developershttps://github.com/sponsors
Security Labhttps://securitylab.github.com
Maintainer Communityhttps://maintainers.github.com
Acceleratorhttps://github.com/accelerator
Archive Programhttps://archiveprogram.github.com
Topicshttps://github.com/topics
Trendinghttps://github.com/trending
Collectionshttps://github.com/collections
Enterprise platformAI-powered developer platformhttps://github.com/enterprise
GitHub Advanced SecurityEnterprise-grade security featureshttps://github.com/security/advanced-security
Copilot for BusinessEnterprise-grade AI featureshttps://github.com/features/copilot/copilot-business
Premium SupportEnterprise-grade 24/7 supporthttps://github.com/premium-support
Pricinghttps://github.com/pricing
Search syntax tipshttps://docs.github.com/search-github/github-code-search/understanding-github-code-search-syntax
documentationhttps://docs.github.com/search-github/github-code-search/understanding-github-code-search-syntax
Sign in https://github.com/login?return_to=https%3A%2F%2Fgithub.com%2FsisterAn%2FJavaScript-Algorithms%2Fissues%2F58
Sign up https://github.com/signup?ref_cta=Sign+up&ref_loc=header+logged+out&ref_page=%2F%3Cuser-name%3E%2F%3Crepo-name%3E%2Fvoltron%2Fissues_fragments%2Fissue_layout&source=header-repo&source_repo=sisterAn%2FJavaScript-Algorithms
Reloadhttps://github.com/sisterAn/JavaScript-Algorithms/issues/58
Reloadhttps://github.com/sisterAn/JavaScript-Algorithms/issues/58
Reloadhttps://github.com/sisterAn/JavaScript-Algorithms/issues/58
sisterAn https://github.com/sisterAn
JavaScript-Algorithmshttps://github.com/sisterAn/JavaScript-Algorithms
Notifications https://github.com/login?return_to=%2FsisterAn%2FJavaScript-Algorithms
Fork 649 https://github.com/login?return_to=%2FsisterAn%2FJavaScript-Algorithms
Star 5.7k https://github.com/login?return_to=%2FsisterAn%2FJavaScript-Algorithms
Code https://github.com/sisterAn/JavaScript-Algorithms
Issues 158 https://github.com/sisterAn/JavaScript-Algorithms/issues
Pull requests 0 https://github.com/sisterAn/JavaScript-Algorithms/pulls
Actions https://github.com/sisterAn/JavaScript-Algorithms/actions
Projects 0 https://github.com/sisterAn/JavaScript-Algorithms/projects
Security Uh oh! There was an error while loading. Please reload this page. https://github.com/sisterAn/JavaScript-Algorithms/security
Please reload this pagehttps://github.com/sisterAn/JavaScript-Algorithms/issues/58
Insights https://github.com/sisterAn/JavaScript-Algorithms/pulse
Code https://github.com/sisterAn/JavaScript-Algorithms
Issues https://github.com/sisterAn/JavaScript-Algorithms/issues
Pull requests https://github.com/sisterAn/JavaScript-Algorithms/pulls
Actions https://github.com/sisterAn/JavaScript-Algorithms/actions
Projects https://github.com/sisterAn/JavaScript-Algorithms/projects
Security https://github.com/sisterAn/JavaScript-Algorithms/security
Insights https://github.com/sisterAn/JavaScript-Algorithms/pulse
New issuehttps://github.com/login?return_to=https://github.com/sisterAn/JavaScript-Algorithms/issues/58
New issuehttps://github.com/login?return_to=https://github.com/sisterAn/JavaScript-Algorithms/issues/58
编程题:用最简洁代码实现 indexOf 方法https://github.com/sisterAn/JavaScript-Algorithms/issues/58#top
编程题https://github.com/sisterAn/JavaScript-Algorithms/issues?q=state%3Aopen%20label%3A%22%E7%BC%96%E7%A8%8B%E9%A2%98%22
https://github.com/sisterAn
https://github.com/sisterAn
sisterAnhttps://github.com/sisterAn
on Jun 2, 2020https://github.com/sisterAn/JavaScript-Algorithms/issues/58#issue-628809975
编程题https://github.com/sisterAn/JavaScript-Algorithms/issues?q=state%3Aopen%20label%3A%22%E7%BC%96%E7%A8%8B%E9%A2%98%22
https://github.com
Termshttps://docs.github.com/site-policy/github-terms/github-terms-of-service
Privacyhttps://docs.github.com/site-policy/privacy-policies/github-privacy-statement
Securityhttps://github.com/security
Statushttps://www.githubstatus.com/
Communityhttps://github.community/
Docshttps://docs.github.com/
Contacthttps://support.github.com?tags=dotcom-footer

Viewport: width=device-width


URLs of crawlers that visited me.