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
Domain: github.com
{"@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-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:b95ded1e-a5a9-fead-88a2-5e9b4f9f4130 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | D9C4:22DEEF:13962A5:1AF22B7:696AA02C |
| html-safe-nonce | acfc14dca413e215fcb3a53bfa0992472739bb458d0dd544278ce284111d5eb3 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJEOUM0OjIyREVFRjoxMzk2MkE1OjFBRjIyQjc6Njk2QUEwMkMiLCJ2aXNpdG9yX2lkIjoiNTc2NDM3MTk3MTYxNjQ0ODU1NyIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 1c319965e89323af1e815759dd63f967ac41b1ad65a0568e596cbd4eb3bf63d8 |
| hovercard-subject-tag | issue:628809975 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/sisterAn/JavaScript-Algorithms/58/issue_layout |
| twitter:image | https://opengraph.githubassets.com/f1815111f22ac3cc499ec6a181feb8ef37409b97d208fdf9d87701cdcce95bda/sisterAn/JavaScript-Algorithms/issues/58 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/f1815111f22ac3cc499ec6a181feb8ef37409b97d208fdf9d87701cdcce95bda/sisterAn/JavaScript-Algorithms/issues/58 |
| og:image:alt | indexOf 有两种: String.prototype.indexOf() 返回从 fromIndex 处开始搜索第一次出现的指定值的索引,如果未找到,返回 -1 str.indexOf(searchValue [, fromIndex]) // fromIndex 默认值为 0 Array.prototype.indexOf() 返回在数组中可以找到一个给定元素的第一个索引,如果不存在... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | sisterAn |
| hostname | github.com |
| expected-hostname | github.com |
| None | a51f97dbb9326f71c08ecb61577457d543c602124d1a2672871258ef37ac5261 |
| turbo-cache-control | no-preview |
| go-import | github.com/sisterAn/JavaScript-Algorithms git https://github.com/sisterAn/JavaScript-Algorithms.git |
| octolytics-dimension-user_id | 19721451 |
| octolytics-dimension-user_login | sisterAn |
| octolytics-dimension-repository_id | 252061924 |
| octolytics-dimension-repository_nwo | sisterAn/JavaScript-Algorithms |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 252061924 |
| octolytics-dimension-repository_network_root_nwo | sisterAn/JavaScript-Algorithms |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 4bd0eac606c70914085176ef312ebdcd97a8cdf1 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width