包装对象Sring的方法与Array的方法
包装对象String的方法
Sting.prototype上的方法
| 方法 | 返回值 | 参数 |
|---|---|---|
| str.substr(start, number)(即将弃用) | 返回从开始下标到指定长度的字符 | start开始下标,可以为负值,负值代number表length+start number截取字符串的个数 |
| str.split(substr:regexp, limit) | 返回以substr:regexp分割而成的数组并保留其中limit位 |
substr:regexp匹配用来分隔的字符串(分隔后这个字符串消失) limit保留的数组长度 |
| str.charAt(index) | 返回指定下标处的字符串 | index:下标 |
| str.search(regexp|substr) | 返回首次匹配的索引;没有则返回 -1 |
`regexp |
| str.replace(regexp|substr, newSubStr|function) | 返回一个被替换过的字符串,如果没参数返回原字符串 | `regexp |
| str.match(regexp|substr) | 使用g标志,返回匹配的结果组成的数组 不使用g标志时,返回一个捕获组,捕获组具有 groups(初始值为undefined),index(开始匹配的下标),input(参与匹配的字符串) |
`regexp |
| str.toLowerCase() | 返回转换为小写的字符串 | |
| str.toUpperCase() | 返回转换为大写的字符串 | |
| str.trim() | 返回一个去除两端空白的字符串 | |
| str.toLocaleUpperCase() | 返回按照本地方式把字符串转换为大写的字符串 | 只有几种语言(如土耳其语)具有地方特有的大小写映射 |
| str.toLocaleLowerCase() | 返回按照本地方式把字符串转换为小写的字符串 | |
| str.substring(start,end) | 返回从开始下标到结束下标的字符串 当start大于end时,相当于end和start互换 |
start开始下标 end结束下标 |
| str.localeCompare(target) | target大于str,返回1target小于str,返回 -1相等,返回 0 |
target参与比较的字符串 |
| str.charCodeAt(index) | 返回指定下标处字符串的Unicode编码 | index:下标 |
| str.startsWith(seastr, start)es6 | 如果在字符串的开头找到了seastr则返回true;否则返回false |
seastr要查找的元素 start开始搜索的下标 |
| str.endsWith(seastr, length)es6 | 如果在字符串的末尾找到了seastr则返回true;否则返回false |
seastr要查找的元素 length参与搜索的长度 |
| str.repeat(count)es6 | 返回字符串被重复count次后的新字符串。 |
count重复的次数 |
Sting上的方法
| 方法 | 作用 | 参数 |
|---|---|---|
| String.fromCharCode(n1,...,nX) | 返回由指定的 Unicode编码创建的字符串 | n1,...,nX要创建的字符串中的字符的 Unicode 编码 |
Array方法
| 方法 | 作用 | 返回值 | 改变否 | 参数讲解 |
|---|---|---|---|---|
| arr.push(str1,..,strn) | 在数组的末尾添加成员 | 返回添加成员后的数组长度 | 改变了原数组 | |
| arr.pop() | 数组末尾删除一个成员 | 返回被删除的成员 | 改变 | |
| arr.unshift(str1,..,strn) | 在数组的开头添加成员 | 返回添加成员后的数组长度 | 改变 | |
| arr.shift() | 在数组的开头删除一个成员 | 返回被删除的成员 | 改变 | |
| arr.splice(index, number, str1, ...,strn) | 可以完成 添加、删除、修改 | 返回被删除的成员组成的数组 | 改变 | number要删除的个数 |
| arr.reverse() | 反转数组 | 返回反转后的数组 | 改变 | |
| arr.join(str) | 以传入的参数为连接符,把数组中的成员连接成一个字符串 | 返回连接后的字符串 | 不改变 | |
| Array.from(arguments) | 将类数组转换为数组 | 返回转化出的新数组 | 不改变 | arguments要转换的类数组 |
| Array.of(str1,...,strn) | 将传入的数据组成一个数组 | 返回创建的数组 |
Array带回调函数的方法
| 方法 | 作用 | 返回值 | 参数 |
|---|---|---|---|
| arr.forEach(function (item, index, currentArr){} | 遍历数组 | item当前遍历的数组成员 index下标(索引) currentArr参与遍历的数组 |
|
| arr.some(function (item, index, currentArr){} | 检测数组成员有没有满足检测函数条件的,数组种成员满足检测函数(函数的返回值为true),就不再继续执行 | 布尔值 | item当前遍历的数组成员 index下标(索引) currentArr参与遍历的数组 |
| arr.every(function (item, index, currentArr){} | 检测每一个数组成员是不是都满足检测函数条件,只要有一个数组成员不满足检测函数条件就结束(返回false) | 布尔值 | item当前遍历的数组成员 index下标(索引) currentArr参与遍历的数组 |
| arr.map(function (item, index, currentArr){} | 将数组成经过一个映射关系转成另一个数组,return 后面的式子就是映射关系 | 返回映射后的数组 | item当前遍历的数组成员 index下标(索引) currentArr参与遍历的数组 |
| arr.filter(function (item, index, currentArr){} | 将数组成员符合条件(满足return条件)的过滤出来 | 返回过滤后的数组,如果没有一个数组成员满足过滤函数的返回的是一个空数组 | item当前遍历的数组成员 index下标(索引) currentArr参与遍历的数组 |
| arr.find(function (item, index, currentArr){} | 在数组中查找符合条件的成员,找到一个符合条件的成员不会继续向后找 | 返回符合条件的成员,找不到返回undefined | item当前遍历的数组成员 index下标(索引) currentArr参与遍历的数组 |
| arr.findIndex(function (item, index, currentArr){} | 在数组中查找符合条件成员的下标,找到一个符合条件的不会继续向后找 | 返回符合条件成员的下标,找不到返回-1 | item当前遍历的数组成员 index下标(索引) currentArr参与遍历的数组 |
数组排序之sort方法
回调函数的参数:数组中的成员
返回排好序后的数组
arr.sort(function (a, b) {
return a - b; //升序
//return b - a; //降序
});
Array.prototype和String.prototype共有的方法
| 方法 | 作用 | 参数 | 改变否 |
|---|---|---|---|
| indexof(seastr,index) | 从index开始查找。返回首个被找到的元素的下标; 若没有找到则返回 -1 | seastr要查找的元素 index开始查找的下标 |
|
| slice(start, end) | 返回从开始下标截取到结束下标的新字符串或数组, 如果没有参数则返回原数组或字符串的拷贝 如果没有end则表示从start截取到字符串或者数组结束 |
start开始下标,可以为负值,负值则表示length+start end结束下标,可以为负值,负值则表示length+end |
不改变 |
| concat(v1,...,vN) | 返回一个新的字符或者数组 | v1,...,vn需要连接的字符串或数组 |
不改变 |
| includes(seastr,start)es6 | 如果当前字符串或数组包含seastr,就返回 true;否则返回 false。 |
seastr要查找的元素 start开始搜索的下标 |
|
| lastIndexOf(str,index) | 从index开始逆向查找。返回首个被找到的元素的下标; 若没有找到则返回 -1 | str要查找的元素 index开始查找的下标 |