{"id":22903516,"url":"https://github.com/sawyerbutton/difference-between-splice-slice-split","last_synced_at":"2025-06-30T16:33:20.990Z","repository":{"id":120749101,"uuid":"152607347","full_name":"sawyerbutton/Difference-between-Splice-Slice-Split","owner":"sawyerbutton","description":null,"archived":false,"fork":false,"pushed_at":"2018-10-11T15:31:28.000Z","size":3,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-02-07T04:41:05.303Z","etag":null,"topics":["functions","javascript","slice","splice","split"],"latest_commit_sha":null,"homepage":null,"language":null,"has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/sawyerbutton.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-10-11T14:42:26.000Z","updated_at":"2018-10-11T15:31:30.000Z","dependencies_parsed_at":null,"dependency_job_id":"f50dd5d7-d742-4c54-9d55-4db277d196bd","html_url":"https://github.com/sawyerbutton/Difference-between-Splice-Slice-Split","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sawyerbutton%2FDifference-between-Splice-Slice-Split","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sawyerbutton%2FDifference-between-Splice-Slice-Split/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sawyerbutton%2FDifference-between-Splice-Slice-Split/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sawyerbutton%2FDifference-between-Splice-Slice-Split/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/sawyerbutton","download_url":"https://codeload.github.com/sawyerbutton/Difference-between-Splice-Slice-Split/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246599012,"owners_count":20803123,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["functions","javascript","slice","splice","split"],"created_at":"2024-12-14T02:37:33.592Z","updated_at":"2025-04-01T07:23:43.354Z","avatar_url":"https://github.com/sawyerbutton.png","language":null,"funding_links":[],"categories":[],"sub_categories":[],"readme":"# Difference between Splice Slice and Split functions in JavaScript\n\n## 消除对于JavaScript中Splice,Slice和Split方法的困惑\n\n\u003e 可能是因为`Splice`,`Slice`,`Split`三种常见的JavaScript方法名称非常类似的原因,这三个方法的使用场景和操作方式经常被开发者混淆\n\n\u003e 希望本文撰写完成后可巩固自身对三种方法的理解\n\n### JavaScript的数组\n\n\u003e JavaScript允许在同一个数组中容纳不同类型的元素,数组可以通过如下方式声明\n\n```javascript\nlet arrayDefinition = [];   // Array declaration in JS\n```\n\n\u003e 对于包含不同类型元素的数组,也可以简单地声明\n\n```javascript\nlet array = [1, 2, 3, \"hello world\", 4.12, true];\n```\n\n### Slice方法\n\n\u003e `slice()`方法复制给定部分的数组,并将复制的部分作为新数组返回\n\n\u003e `slice()`方法不会修改原数组\n\n```bash\narray.slice(from, until);\n```\n\n- from:从元素索引开始切割数组\n- until:将数组切歌至直到另一个元素索引\n\n\u003e 假设希望切割数组的前三个元素\n\n```javascript\nlet array = [1,2,3,4,5];\nlet newArray = array.slice(0,3);\nconsole.log(newArray);\n// 1,2,3\n```\n\n\u003e 值得注意的是unitl所在索引所代表的元素并不包含在被切割的数组中,亦即前包后不包原则\n\n\u003e `slice()`方法也可以用于String类型的变量\n\n```javascript\nlet testString = '12345';\nlet newString = testString.slice(0,3);\nconsole.log(newString);\n// 123\n```\n\n### Splice方法\n\n\u003e `splice()`方法通过修改或删除元素的方式修改原数组\n\n#### 移除元素\n\n```bash\narray.splice(index, number_of_elements);\n```\n\n- index是删除元素的起点,索引编号小于index的元素将不会被删除\n- number_of_elements是将被删除的元素的个数\n\n```javascript\narray.splice(2);  // 索引大于等于2的元素都将会被删除\n```\n\n\u003e 需要注意的是,如果不添加参数`number_of_elements`,所有从给定的索引位置开始的元素都贱会被删除\n\n```javascript\nlet array = [1,2,3,'hello',4.5,true];\nlet deletedArray = array.splice(2,1,);\nconsole.log(deletedArray) //[3]\nconsole.log(array) // [1,2,'hello',4.5,true]\n```\n\n\u003e 在存在第二个参数的情况下,将会从指定索引位置开始删除指定量个元素,循环调用时将会在给定的索引目录本身元素不存在时结束\n\n### 增加元素\n\n\u003e 需要添加元素时,需要向`splice()`方法提供可能存在的第三,第四...第n个元素\n\n\u003e 假设希望在数组的开头增加`a`,`b`两个元素\n\n```javascript\nlet array = [1,2,3,'hello',4.5,true];\narray.splice(0,0,'a','b');\n// 增加元素的操作将会返回空数组\nconsole.log(array);\n// [a,1,2,3,'hello',4.5,true]\n```\n\n## Split方法\n\n\u003e 相较之于`splice()`和`slice()`方法大多数情况下作用于Array类型的情况下, `split()`方法只能作用于String类型的变量上\n\n\u003e `split()`方法将一个字符串分成子串并将它们组合为数组返回\n\n```bash\nstring.split(separator, limit);\n```\n\n- separator: 定义了以什么方式分割字符串\n- limit: 定义了分割的次数\n\n```javascript\nlet array = [1,2,3,'hello',4.5,true];\nlet myString = array.toString();\n// myString '1,2,3,hello,4.5,true'\nlet newArray = myString.split(\",\", 3);\n// newArray ['1','2','3']\n```\n\n\u003e myString以`,`为标准切割字符串并保留前三个元素\n\n\u003e 值得注意的是,如果使用`myString.split(\"\")` 切割字符串,字符串将会以每个字符为分割线将每个字符作为一个子字符串切割\n\n## 总结\n\n- Slice()\n\n1. 从数组中拷贝元素\n2. 返回新数组\n3. 不修改原数组\n4. 前包尾不包的切割方式\n5. 既可以应用于数组也可以应用于字符串\n\n- Splice()\n\n1. 向数组中添加或删除元素\n2. 删除时返回被删除的元素构成的数组\n3. 添加时返回空数组\n4. 修改原数组\n5. 只可以应用于数组类型变量\n\n- Spilt()\n\n1. 将字符串变量拆分为字串数组\n2. 返回值为数组类型\n3. 包含两个可选参数`separator`和`limit`\n4. 不会需改原字串\n5. 只可以应用于字符串类型","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsawyerbutton%2Fdifference-between-splice-slice-split","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsawyerbutton%2Fdifference-between-splice-slice-split","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsawyerbutton%2Fdifference-between-splice-slice-split/lists"}