1:substring 和 substr数组
var str = '0123456789' console.log(str.substring(1)); //123456789 console.log(str.substr(1)); //123456789 console.log(str.substr(2,5));// 234 区间为[2, 5) console.log(str.substr(2, 5));// 23456 区间为[2, 2+5-1]; console.log(-4);// 6789截取后四位 console.log(str) 原始字符串不变
2: slice 和 splicecode
二者都是针对数组的,slice适用于字符串 splice是直接在原始数组上操做的(直接在数组上操做的还有pop(数组末尾删除)/push(数组末末尾添加)/shift(数组首部删除)/unshift(数组首部添加)/sort/reverse/concat) var arr = [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ]; console.log(arr.slice(1)); // [1, 2, 3, 4, 5, 6, 7, 8, 9]; console.log(arr.splice(1)); // [1, 2, 3, 4, 5, 6, 7, 8, 9]; console.log(arr) ; // [0], 被splice砍下的。 arr = [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ]; console.log(arr.slice(2, 5)); // [2, 3, 4] 区间为[2, 5) console.log(arr.splice(2, 5)); // [2, 3, 4, 5, 6] 区间为[2, 2+5-1]; console.log(arr); // [0, 1, 7, 8, 9]; 记忆: splice里面有一个p,看起来就像一把刀,直接砍在数组身上。
3:数组扩展:对象
3.1: 扩展运算符... 3.2: Array.from($1) 将一个相似数组的对象转换为一个数组。 3.3:Array.of($1, $2, $3) 将一组值转换为一个数组。 3.4:Array.copyWithin($1, $2, $3) 表示将索引为$2到索引为$3之间的成员复制到索引为$1的位置上面。 3.5:Array.find((n) => { n > 10}); 表示找到第一个符合条件的数组成员。 3.6:Array.findIndex((n) => { n< 10}) 表示找到第一个符合条件的数组值得索引下标。 3.7:Arrray.fill() // 表示填充一个数组。 ['a', 'b'. 'c'].fill(7) [7, 7, 7] ['a', 'b', 'c'].fill(7, 1, 2) ['a', 7, 'c'] 3.8: Array.entries()表示对键值对的遍历/Array.keys()表示对键名的遍历/Array.values()表示对键值的遍历 3.9:Array.includes($1) 表示数组中是否包含$1这个元素值。 3.10:ForEach(让数组中的每一项作一件事) arr.forEach((item, index) => { console.log(item) }) 3.11:map(让数组经过某种计算产生一个) arr.map((item,index) => { return item * 10}) 3.12: filter(筛选出数组中符合条件的项,组成新数组) arr.filter((item, index) => { return item > 3}) 3.13:reduce(让数组中的前项和后项作某种计算,并累计最总值) arr.reduce((prev, next) => { return prev + next}) 3.14: every(检测数组中的每一项是否符合条件) arr.every((item, index) => { return item> 0}) 返回true或者false 3.15:some(检测数组中是否有条件符合条件) arr.some((item, index) => { return item > 0}) 返回true或者false
4:字符串的扩展?索引
4.1:string.at($1) 返回字符串给定位置的字符 4.2:string.includes($1) 表示是否找到了参数字符串 4.3:string.startsWith($1) 表示参数字符串是否在源字符串的头部 4.4:string.endsWith($1) 表示参数字符串是否在源字符串的结尾处 includes/startsWith/endsWith 支持第二个参数$2 includes/startsWith 第二个参数表示从第$2个位置到字符串结束位置之间的字符 endsWith第二个参数表示前$2个字符串中。 4.5: string.repeat(n)方法返回一个新的字符串表示元字符重复n次。 4.6: string.padStart(n, $1)/string.padEnd(n, $1) 字符串补全长度 n表示字符串的最小长度 $1表示要补全的字符串。 ‘x’.padStart(5, 'ab') // 'ababx' 'x'.padStart(4, 'ab') // 'abax' 'x'.padEnd(5, 'ab')// 'xabab' 'x'.padend(4, 'ab') // 'xaba'