split,splice,slice 三者的用法
很多知识点不熟悉可以自行去下面链接查询:
mdn web docs
最近一直忙于搞python,等后续有时间更新python相关的内容。毕竟现在在弄web.有些知识点需要巩固,以便自己后续带人和巩固自己的知识体系。
一.split
split() 方法使用指定的分隔符字符串将一个String对象分割成子字符串数组。
const str2 = 'The quick brown fox jumps over the lazy dog.';
let arr1 = str2.split(" "); // spilit slice 切割数组
console.log(arr1); //(9) ['The', 'quick', 'brown', 'fox', 'jumps', 'over', 'the', 'lazy', 'dog.']
二.slice
slice(beginIndex[, endIndex])可根据坐标位置进行删除数组并返回长度,返回一个新数组但不会影响原有的数组。
const str2 = 'The quick brown fox jumps over the lazy dog.';
let arr1 = str2.split(" "); // spilit slice 切割数组
console.log(arr1); //(9) ['The', 'quick', 'brown', 'fox', 'jumps', 'over', 'the', 'lazy', 'dog.']
let ch = arr1.slice(0,4)
console.log(ch); // ['The', 'quick', 'brown', 'fox']
console.log(arr1)//['The', 'quick', 'brown', 'fox', 'jumps', 'over', 'the', 'lazy', 'dog.']
三.splice
slice(index,num,item)第一个坐标位置,第二个要删除的项目数量。如果设置为 0,则不会删除项目,第三个向数组添加的新项目,会影响原有数组
const str2 = 'The quick brown fox jumps over the lazy dog.';
let arr1 = str2.split(" "); // spilit slice 切割数组
console.log(arr1); //(9) ['The', 'quick', 'brown', 'fox', 'jumps', 'over', 'the', 'lazy', 'dog.']
let ch = arr1.splice(0,4)
console.log(ch); // ['The', 'quick', 'brown', 'fox']
console.log(arr1)//['jumps', 'over', 'the', 'lazy', 'dog.']