Title: 移动元素函数里 判断over<0的情况 i应该从startIdx+delCount+over开始 · Issue #168 · sisterAn/JavaScript-Algorithms · GitHub
Open Graph Title: 移动元素函数里 判断over<0的情况 i应该从startIdx+delCount+over开始 · Issue #168 · sisterAn/JavaScript-Algorithms
X Title: 移动元素函数里 判断over<0的情况 i应该从startIdx+delCount+over开始 · Issue #168 · sisterAn/JavaScript-Algorithms
Description: 移动元素函数里 判断over<0的情况 i应该从startIdx+delCount+over开始 /** 数组splice方法实现 arr.splice(startIdx,delCount,add1,add2,...); */ Array.prototype._splice = function () { //1. 记录入参个数 let argumentsLen = arguments.length; let start = arguments[0], deleteCo...
Open Graph Description: 移动元素函数里 判断over<0的情况 i应该从startIdx+delCount+over开始 /** 数组splice方法实现 arr.splice(startIdx,delCount,add1,add2,...); */ Array.prototype._splice = function () { //1. 记录入参个数 let argumentsLen = arguments.le...
X Description: 移动元素函数里 判断over<0的情况 i应该从startIdx+delCount+over开始 /** 数组splice方法实现 arr.splice(startIdx,delCount,add1,add2,...); */ Array.prototype._splice = function () { //1. 记录入参个数 let argumentsLen = arguments...
Opengraph URL: https://github.com/sisterAn/JavaScript-Algorithms/issues/168
X: @github
Domain: patch-diff.githubusercontent.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"移动元素函数里 判断over\u003c0的情况 i应该从startIdx+delCount+over开始","articleBody":"```js\r\n移动元素函数里 判断over\u003c0的情况 i应该从startIdx+delCount+over开始\r\n/**\r\n数组splice方法实现\r\n\r\narr.splice(startIdx,delCount,add1,add2,...);\r\n*/\r\nArray.prototype._splice = function () {\r\n//1. 记录入参个数\r\nlet argumentsLen = arguments.length;\r\nlet start = arguments[0], deleteCount = arguments[1];\r\n//2. 数组长度\r\nlet arrayLength = this.length;\r\nlet arr = Object(this);\r\n//3. 添加元素个数\r\nlet addCount = argumentsLen \u003e 2 ? argumentsLen - 2 : 0\r\nconsole.log('addCount: ', addCount);\r\n//4. 计算有效的开始位置start\r\nlet startIdx = computeSpliceStartIdx(start, arrayLength);\r\n//5. 计算有效的删除个数\r\nlet delCount = computeSpiceDelCount(startIdx, deleteCount, arrayLength);\r\n\r\nconsole.log('delCount: ', delCount);\r\n//6. 记录删除的元素\r\nlet delElements = new Array(delCount);\r\nrecordDelElements(startIdx, delCount, arr, delElements);\r\n//7. 判断是否是密封对象\r\nif (delCount !== addCount \u0026\u0026 Object.isSealed(arr)) {\r\nthrow new TypeError('the arr is sealed')\r\n}\r\n//8. 判断是否是冻结对象\r\nif (delCount \u003e 0 \u0026\u0026 addCount \u003e 0 \u0026\u0026 Object.isFrozen(arr)) {\r\nthrow new TypeError('the arr is frozen')\r\n}\r\n//移动数组元素\r\nmoveElements(startIdx, delCount, arr, addCount);\r\nlet i = startIdx;\r\nlet argumentsIdx = 2;\r\n//插入新的元素\r\nwhile (argumentsIdx \u003c argumentsLen) {\r\narr[i++] = arguments[argumentsIdx++];\r\n}\r\narr.length = arrayLength - delCount + addCount;\r\nreturn delElements;\r\n}\r\nfunction computeSpliceStartIdx(start, arrayLength) {\r\nif (start \u003c 0) {\r\nstart += arrayLength;\r\nreturn start \u003c 0 ? 0 : start;\r\n}\r\n//start\u003e0的情况\r\nreturn start \u003e arrayLength - 1 ? arrayLength - 1 : start;\r\n}\r\n//计算delCount\r\nfunction computeSpiceDelCount(startIdx, deleteCount, arrayLength) {\r\nif (deleteCount \u003e arrayLength - startIdx) {\r\ndeleteCount = arrayLength - startIdx\r\n}\r\nif (deleteCount \u003c 0) deleteCount = 0\r\nreturn deleteCount;\r\n}\r\n//记录删除的元素\r\nfunction recordDelElements(startIdx,delCount,arr,delElements) {\r\nfor(let i=0;i\u003cdelCount;i++) {\r\ndelElements[i] = arr[startIdx+i];\r\n}\r\n}\r\n//移动数组\r\nfunction moveElements(startIdx, delCount, arr, addCount){\r\nlet over = addCount - delCount;\r\nconsole.log('over: ', over);\r\nif(over\u003e0) {\r\n//增加的数大于了删除的数 向后移动\r\nfor(let i=arr.length-1;i\u003e=startIdx+delCount;i--) {\r\narr[i+over] = arr[i];\r\n}\r\n} else if(over\u003c0) {\r\n//增加的数小于删除的数 向前移动\r\nfor(let i=startIdx+delCount+over;i\u003c=arr.length-1;i++) {\r\nif(i+Math.abs(over)\u003earr.length-1) {\r\ndelete arr[i]\r\ncontinue\r\n}\r\narr[i] = arr[i+Math.abs(over)];\r\n}\r\nconsole.log('arr: over\u003c0', arr);\r\n}\r\n}\r\nlet arr = [1, 2, 3, 4, 5];\r\narr._splice(1,3,6,7)\r\nconsole.log('arr: ', arr);//arr: [ 1, 6, 7, 5 ]\r\n```\r\n\r\n_Originally posted by @xllpiupiu in https://github.com/sisterAn/JavaScript-Algorithms/issues/138#issuecomment-932177874_","author":{"url":"https://github.com/xllpiupiu","@type":"Person","name":"xllpiupiu"},"datePublished":"2021-10-01T12:20:18.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":0},"url":"https://github.com/168/JavaScript-Algorithms/issues/168"}
| 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:66b0d5dd-2cc3-416f-3d32-4b5cd35d9785 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | B8CE:456F1:17A3470:20E7448:69727580 |
| html-safe-nonce | e1363153628ba3517e4cc62ebf6b9ed66fa310e490b0f5b1d0864b5a5bd47325 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCOENFOjQ1NkYxOjE3QTM0NzA6MjBFNzQ0ODo2OTcyNzU4MCIsInZpc2l0b3JfaWQiOiI3NzE2NDI2MDY0OTU5MDEwMTc2IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | f38d979b40e388d479e9b714a68e30aa53e8321a055829fd012425d0ee653b01 |
| hovercard-subject-tag | issue:1013281144 |
| 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/168/issue_layout |
| twitter:image | https://opengraph.githubassets.com/55f1305c6c40c6975f16a76cc3407ce4faa9bd9aaafd3e21ad89281f7f70ff3f/sisterAn/JavaScript-Algorithms/issues/168 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/55f1305c6c40c6975f16a76cc3407ce4faa9bd9aaafd3e21ad89281f7f70ff3f/sisterAn/JavaScript-Algorithms/issues/168 |
| og:image:alt | 移动元素函数里 判断over<0的情况 i应该从startIdx+delCount+over开始 /** 数组splice方法实现 arr.splice(startIdx,delCount,add1,add2,...); */ Array.prototype._splice = function () { //1. 记录入参个数 let argumentsLen = arguments.le... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | xllpiupiu |
| hostname | github.com |
| expected-hostname | github.com |
| None | 3be355d8ea7ca74c4edec03a70ecaa8211862649e90886d88a4db13e6778e4c3 |
| 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 | 4ae5b6c1e16519c3e3ad37a388f7cca2192a79a8 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width