JS常用操作方法图表
截取字符串方法
| 方法名 | 参数 | 返回值 | 例子 |
|---|---|---|---|
| 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.prototype.slice.call({ 0: 'a', 1: 'b', length: 2 }) //['a', 'b']
- Array.from也可以做到
Array.from({ 0: 'a', 1: 'b', length: 2 }) //['a', 'b']
常用数组方法
var arr = [1, 2, 3];
let arr2 = ['今天天气不错', '早上好']
arr2 = arr2.map(s => s.split(''))
// [ [ '今', '天', '天', '气', '不', '错' ], [ '早', '上', '好' ] ]
| 方法名 | 参数和说明 | 返回值 | 例子 |
|---|---|---|---|
| Array.isArray() | arr | Boolean | Array.isArray(arr) // true |
| Array.prototype.valueOf() | 无 | 数组本身 | arr.valueOf() // [1, 2, 3] |
| Array.prototype.toString() | 无 | 数组的字符串形式 | arr.toString() // "1,2,3" |
| Array.prototype.push() | elementN,尾部插入值 | 新的length | arr.push(7) // 4 |
| Array.prototype.pop() | 无 | 删除的元素,删除最后一个元素 | arr.pop() // 3 |
| Array.prototype.shift() | 无 | 删除的元素,删除第一个元素 | arr.shift() // 1 |
| Array.prototype.unshift() | elementN,首部插入值 | 新的length | arr.shift(7) // 4 |
| Array.prototype.join() | separator,默认为'',' | 字符串 | arr.join(' |
| 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,否则返回false | Boolean | arr.some(function (item) { return item > 2 }); //true |
| Array.prototype.every() | 回调函数,所有元素满足条件即返回true,否则返回false | Boolean | arr.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)第二个参数是开始查找的位置 | 存在返回索引值,没有返回-1 | arr.indexOf(2); //1 |
| Array.prototype.lastIndexOf() | (searchElement,fromIndex)第二个参数是开始查找的位置,从右到左 | 存在返回索引值,没有返回-1 | arr.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() | regexp | array | '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常用操作方法图表的更多相关文章
- JS常用操作方法
1.splice() 方法向/从数组中添加/删除项目,然后返回被删除的项目. 注释:该方法会改变原始数组. 1 <script type="text/javascript"& ...
- js数组常用操作方法小结(增加,删除,合并,分割等)
本文实例总结了js数组常用操作方法.分享给大家供大家参考,具体如下: var arr = [1, 2, 3, 4, 5]; //删除并返回数组中第一个元素 var theFirst = arr.shi ...
- js常用工具类.
一些js的工具类 复制代码 /** * Created by sevennight on 15-1-31. * js常用工具类 */ /** * 方法作用:[格式化时间] * 使用方法 * 示例: * ...
- chart.js 里添加图表的清单:
chart.js 里添加图表的清单: var legend = myDoughnut.generateLegend(); $("#chart_legend").html(legen ...
- Js常用技巧
摘录:http://crasywind.blog.163.com/blog/static/7820316920091011643149/ js 常用技巧 1. on contextmenu=" ...
- JS常用的标准函数
原文:JS常用的标准函数 1.Array类型函数 array.concat(item...) 函数功能:关联数组,实现数组相加功能,但并不影响原先数组,concat返回新数组. array.join( ...
- JS 常用功能收集
JS 常用效果收集 1. 回到顶部>> 爱词霸
- JS常用校验方法(判断输入框是否为空,数字,电话,邮件,四舍五入等)
JS常用校验方法: 1.判断输入框是否为空,为空时弹出提示框 2.关闭窗口 3.检查输入字符串是否为数字 4.强制把大写转换成小写 5.手机号码校验,长度为11位数字. 6.电子邮件校验 7.电话号码 ...
- Node.js 常用工具
Node.js 常用工具 util 是一个Node.js 核心模块,提供常用函数的集合,用于弥补核心JavaScript 的功能 过于精简的不足. util.inherits util.inherit ...
随机推荐
- mysql-cluster 7.3.5安装部署
集群环境 管理节点 10.0.0.19 数据节点 10.0.0.12 10.0.0.17 sql节点 10.0.0.18 10.0.0.22 添加mysql用户 groupadd mysql user ...
- Python3基础 os chdir 改变工作目录
Python : 3.7.0 OS : Ubuntu 18.04.1 LTS IDE : PyCharm 2018.2.4 Conda ...
- hbase读写流程分析
前言 最近被大佬问到一个问题,hbase查询数据在最坏的场景下需要进行几次rpc,当时就懵了..下面主要对client端代码进行分析.阅读文章和看源码更配~ 读数据 流程总览 1. 从zookeepe ...
- mysql时间格式化函数日期格式h和H区别
本文为博主原创,未经允许不得转载: 今天碰到一个问题,发现项目中有一个统计图的数据和时间格式没有对应准确,统计图要描述的是操作次数和操作时间的关系, 但很奇怪的是操作次数对应的时间却是凌晨,实际应用中 ...
- NYOJ 16 矩形嵌套(经典DP)
http://acm.nyist.net/JudgeOnline/problem.php?pid=16 矩形嵌套 时间限制:3000 ms | 内存限制:65535 KB 难度: ...
- 2:JavaScript中的基本运算
今天说的是JavaScript中的数据基本运算 在上一节中已经说了关于JavaScript中的基本数据类型 那么数据有了 剩下来就是数据之间的运算 表达式-------预算符(赋值 比较 算数 逻辑 ...
- Linux——进程管理简单学习笔记(二)
计划任务: 为什么要设置计划任务 : 实现数据库备份,发送系统通知神马的..... 计划任务的命令: 一: at : 安排作业在某一时刻执行一次 nbatch 安排作业在系统负载不重时执 行一 ...
- testNG 学习笔记 Day 1 使用功能详解
TestSuite处理测试用例有6个规约(否则会被拒绝执行测试) A 测试用例必须是公有类(Public) B 测试用例必须继承与TestCase类 C 测试用例的测试方法必须是公有的( Public ...
- MongoDB(课时20 游标)
3.5 游标(重点) 所谓游标就是指数据可以一行行的进行操作,非常类似于ResultSet数据处理.在MongoDB里对游标的控制使用find()函数就可以返回游标.对于返回的游标如果想进行操作,使用 ...
- Beta 冲刺 第三天
第三天 2018.6.26 今日完成任务情况. 妥志福.牛瑞鑫: 完成任务:让用户接触系统,指出系统中存在的问题,并统计修改的问题,提出修改的方案. 王胜海.马中林: 完成任务:对文档中存在的问题修改 ...