截取字符串方法

String.prototype.substr()(indexStart, length) 都可为负数,length为负数时自动转为0,length不传默认截取剩下的所有新字符串‘JavaScript’.substr(4, 6) // “Script”
String.prototype.substring()(indexStart, indexEnd) 负数会转为0新字符串‘JavaScript’.substring(-4) // “JavaScript”
String.prototype.slice()(indexStart, indexEnd) 负数会转为与长度相加之后的数新字符串‘JavaScript’.slice(-4) // “ript”

三者区别:

  • 他们三者第二个参数都是可选的,如果只传一个正常下标的数字,都是返回从这个下标开始到字符串结束的这一段字符串,他们都不会改变原字符串
  • substr截取的是从indexStart下标开始长度为length的字符串,substring和slice两个参数都是开始和结束的下标,并且都不包含结束下标的字符
  • substring和slice参数如果为负数,substring会转为0,而slice会转为与字符串的长度相加之后的新下标

截取数组方法

Array.prototype.slice()(indexStart, indexEnd) 如果slice方法的参数是负数,则表示倒数计算的位置新数组[‘a’, ‘b’, ‘c’].slice(1, 2) //[‘b’]
Array.prototype.splice()(indexStart, count, addElement1, addElement2,…) 如果只传一个参数就相当于把原数组拆分为截断了被删除的元素,改变原数组[‘a’, ‘b’, ‘c’, ‘d’, ‘e’, ‘f’].splice(4, 2); // [“e”, “f”]

两者区别:

  • 只传一个正常下标,都能得到从这个下标开始到数组结束的一个新数组
  • slice不会改变原数组,返回值是新数组,splice会改变,返回值是被删除的元素
  • slice能够删除原数组中的某些元素,并能在删除的位置添加新成员

tips:

  • slice方法还有一个重要的应用,可以将类数组对象转为数组

  • Array.from也可以做到

常用数组方法

Array.isArray()arrBooleanArray.isArray(arr) // true
Array.prototype.valueOf()数组本身arr.valueOf() // [1, 2, 3]
Array.prototype.toString()数组的字符串形式arr.toString() // “1,2,3”
Array.prototype.push()elementN,尾部插入值新的lengtharr.push(7) // 4
Array.prototype.pop()删除的元素,删除最后一个元素arr.pop() // 3
Array.prototype.shift()删除的元素,删除第一个元素arr.shift() // 1
Array.prototype.unshift()elementN,首部插入值新的lengtharr.shift(7) // 4
Array.prototype.join()separator,默认为’’,’字符串arr.join(‘‘) // “123”
Array.prototype.concat()array,object,elementN新数组(浅拷贝)arr.concat([7]) // [1, 2, 3, 7]
Array.prototype.reverse()颠倒排序之后的原数组arr.reverse() // [3, 2, 1]
Array.prototype.sort()无,默认按字典排序,或者传入一个函数重新排序之后的原数组arr.sort() // [1, 2, 3]
Array.prototype.map()回调函数,把每次的执行结果重新组成一个数组新数组arr.map(function (n) { return n + 1; }); //[2,3,4]
Array.prototype.forEach()回调函数,操作原数组不返回值,改变原数组arr.forEach(function (n) { console.log(n) }); //1 2 3
Array.prototype.filter()回调函数,根据筛选项条件得到新数组新数组arr.filter(function (item) { return (item > 2) }); //[3]
Array.prototype.some()回调函数,某一个元素满足条件即返回true,否则返回falseBooleanarr.some(function (item) { return item > 2 }); //true
Array.prototype.every()回调函数,所有元素满足条件即返回true,否则返回falseBooleanarr.every(function (item) { return item > 2 }); //false
Array.prototype.reduce()(func, 初始值),从左到右执行之后的返回值arr.reduce(function (prev, cur) { return prev+cur }, 10); //16
Array.prototype.reduceRight()(func, 初始值),从右到左执行之后的返回值arr.reduceRight(function (prev, cur) { return prev+cur }, 10); //16
Array.prototype.indexOf()(searchElement,fromIndex)第二个参数是开始查找的位置存在返回索引值,没有返回-1arr.indexOf(2); //1
Array.prototype.lastIndexOf()(searchElement,fromIndex)第二个参数是开始查找的位置,从右到左存在返回索引值,没有返回-1arr.lastIndexOf(3); //2
ES6
Array.from()类似数组的对象和可遍历的对象真正的数组Array.from({‘0’: ‘a’,’1’: ‘b’,’2’: ‘c’, length: 3}); // [“a”, “b”, “c”]
Array.of()将一组值转为数组,为了弥补Array()参数不同的行为差异数组Array.of(1,2,3); // [1, 2, 3]
Array.prototype.includes()(searchElement,fromIndex),fromIndex可选,便是开始查找的位置Boolean[1, 2, 3].includes(2); ; // true
Array.prototype.flat()depth,可选参数,默认1,将嵌套数组拉平变成一维数组新数组[1, 2, [3, 4, [5, 6]]].flat(2); // [1, 2, 3, 4, 5, 6]
Array.prototype.flatMap()callback,新数组arr2.flatMap(s => s.split(‘’)); // [“今”, “天”, “天”, “气”, “不”, “错”, “早”, “上”, “好”]
Array.prototype.copyWithin()(target, start, end) 将start和end之间的元素复制覆盖target指定的位置,end指定的元素不会复制数组[1,2,3,4].copyWithin(1, 2, 3); // [1, 3, 3, 4]
Array.prototype.find()(callback, thisArg)返回符合条件的第一个值,否则unfettered[1,2,3,4,5].find(function(item){ return item>2 }); // 3
Array.prototype.findIndex()(callback, thisArg)返回符合条件的第一个值的下标,否则unfettered[1,2,3,4,5].findIndex(function(item){ return item>2 }); // 2
Array.prototype.fill()(value, start, end ),将指定的位置填充为value,如果不指定就全部填充修改后的数组[1,2,3,4].fill(7, 2, 3); // [1, 2, 7, 4]
Array.prototype.entries()可遍历的Iterator对象,[key, value][1,2,3,4].entries(); // 可被for of遍历
Array.prototype.keys()可遍历的Iterator对象, key[1,2,3,4].keys(); // 可被for of遍历
Array.prototype.values()可遍历的Iterator对象, value[1,2,3,4].values(); // 可被for of遍历

字符串处理方法

String.fromCharCode()一个或多个数值,代表Unicode 码点字符串String.fromCharCode(104, 101, 108, 108, 111) // ‘hello’
String.prototype.charAt()index,下标字符串new String(‘abc’).charAt(1) //b
String.prototype.charCodeAt()index,下标Unicode 码点(十进制表示)‘hello’.charCodeAt(1) // 101
String.prototype.concat()string2…stringN字符串‘hello’.concat(‘world’, ‘haha’) // “helloworldhaha”
String.prototype.indexOf()searchValue,fromIndex第一次出现的索引,没找到就-1‘hello’.indexOf(‘e’) // 1
String.prototype.lastIndexOf()searchValue,fromIndex第一次出现的索引‘wanwan’.lastIndexOf(‘a’); //4 ‘wanwan’.lastIndexOf(‘a’, 3) //1
String.prototype.trim()无,删除字符串两端空白字符串新字符串‘ wan ‘.trim() // ‘wan’
String.prototype.toLowerCase()无,小写新字符串‘wanCheng’.toLowerCase() // ‘wancheng’
String.prototype.toUpperCase()无,大写新字符串‘wanCheng’.toUpperCase() // ‘WANCHENG’
String.prototype.match()regexparray‘wancheng’.match(‘wan’) // [‘wan’] ‘wancheng’.match(/w/) // [‘w’]
String.prototype.search()regexp首次匹配成功的索引,不成功为-1‘wancheng’.search(‘c’) //
String.prototype.replace()regexp新字符串‘wan cheng’.replace(/wan/i, ‘san’) // “san cheng”
ES6
String.prototype.includes()searchString,position(可选)boolean‘hello world’.includes(‘hello’) // true
String.prototype.startsWith()searchString,position(可选),是否是以给定的字符串开头boolean‘hello world’.startsWith(‘h’) // true
String.prototype.endsWith()searchString,position(可选),是否是以给定的字符串结尾boolean‘hello worlds’.endsWith(‘s’) // true
String.prototype.repeat()count([0, +∞])新字符串‘wan’.repeat(2) // ‘wanwan’
String.prototype.padStart()targetLength,padString (可选)原字符串‘abc’.padStart(7, ‘sd’) // “sdsdabc”
String.prototype.padEnd()targetLength,padString (可选)原字符串‘abc’.padEnd(7, ‘sd’) // “abcsdsd”

原文:大专栏  JS常用操作方法图表


01-15 03:50
查看更多