数组的常用方法
push()
向数组的末尾添加新类容
参数:要添加的项。传递多个用逗号隔开,任何数据类型都可以 返回值:新增后数组的长度 是否改变原数组:改变
![](https://img-blog.csdnimg.cn/img_convert/d54817fca5c11b9b3cf545737c5ae806.png)
![](https://img-blog.csdnimg.cn/img_convert/8bc8d6eaf1c5b5ea372300885ac9b851.png)
pop()
删除数组的最后一项
参数:无
返回值:被删除的项
是否改变原数组:改变
![](https://img-blog.csdnimg.cn/img_convert/4ed80d640f1d35420f1669d7ce48ea4e.png)
![](https://img-blog.csdnimg.cn/img_convert/0bb745f7e9ecffb4561b36b5d67f7b87.png)
shift()
删除数组的第一项
参数:无
返回值:被删除的项
是否改变原数组:改变
![](https://img-blog.csdnimg.cn/img_convert/14aca8a4efb53e21e494002116a5d05c.png)
![](https://img-blog.csdnimg.cn/img_convert/1ce9514f73c0bc61a570a27464c90007.png)
unshift()
向数组首位添加新内容
参数:要添加的项,多项用','隔开
返回值:新数组的长度
是否改变原数组:改变
![](https://img-blog.csdnimg.cn/img_convert/939b616a0e0c170fafe175656aa3fcb6.png)
slice()
按照条件查找出其中的部分内容
参数:
array.slice(n, m),从索引n开始查找到m处(不包含m)
array.slice(n) 第二个参数省略,则一直查找到末尾
array.slice(0)原样输出内容,可以实现数组克隆
array.slice(-n,-m) slice支持负参数,从最后一项开始算起,-1为最后一项,-2为倒数第二项
返回值:返回一个新数组
是否改变原数组:不改变
![](https://img-blog.csdnimg.cn/img_convert/0a5b123af79937ebb724d8ad99793b21.png)
![](https://img-blog.csdnimg.cn/img_convert/7f1911d17057c92fcae81f159d463b67.png)
splice
对数组进行增删改
增加:ary.splice(n,0,m)从索引n开始删除0项,把m或者更多的内容插入到索引n的前面
返回空数组
修改:ary.splice(n,x,m)从索引n开始删除x个,m替换删除的部分
把原有内容删除掉,然后用新内容替换掉
删除:ary.splice(n,m) 从索引n开始删除m个内容
(如果第二个参数省略,则从n删除到末尾)
返回删除的新数组,原有数组改变
![](https://img-blog.csdnimg.cn/img_convert/bb8c8e47b2f8ae592f0cc7c930eb05e2.png)
![](https://img-blog.csdnimg.cn/img_convert/7a8345c3461f65ba2c04a82230cd9eb1.png)
join()
用指定的分隔符将数组每一项拼接为字符串
参数:指定的分隔符(如果省略该参数,则使用逗号作为分隔符)
返回值:拼接好的字符串
是否改变原数组:不改变
![](https://img-blog.csdnimg.cn/img_convert/fad2e4192dd97bbfd6e607ade3e8929b.png)
![](https://img-blog.csdnimg.cn/img_convert/5079d639f4d17a33d6633f48b0cfb1f9.png)
concat()
用于连接两个或多个数组
参数:参数可以是具体的值,也可以是数组对象。可以是任意多个
返回值:返回连接后的新数组
是否改变原数组:不改变
![](https://img-blog.csdnimg.cn/img_convert/17232c0c29962b03384c333b57367a9b.png)
![](https://img-blog.csdnimg.cn/img_convert/db6f80f026c02678ec2962c9adafa5cd.png)
indexOf()
检测当前值在数组中第一次出现的位置索引
参数:array.indexOf(item,start) item:查找的元素 start:字符串中开始检索的位置
返回值:第一次查到的索引,未找到返回-1
是否改变原数组:不改变
![](https://img-blog.csdnimg.cn/img_convert/2680325a96d4aea1eab97757eb2c41ec.png)
![](https://img-blog.csdnimg.cn/img_convert/d8cce132f42e5b0334558635bb92693a.png)
lastindexOf()
检测当前值在数组中最后一次出现的位置索引
参数:array.lastIndexOf(item,start) item:查找的元素 start:字符串中开始检索的位置
返回值:第一次查到的索引,未找到返回-1
是否改变原数组:不改变
![](https://img-blog.csdnimg.cn/img_convert/e1d77820cd85a10e3a66c819870bfd36.png)
![](https://img-blog.csdnimg.cn/img_convert/bc3ff187a67d26cc3abfe72f5a1239ac.png)
includes()
判断一个数组是否包含一个指定的值
参数:指定的内容
返回值:布尔值
是否改变原数组:不改变
![](https://img-blog.csdnimg.cn/img_convert/336d5686f75945a076dd017ca49d4209.png)
![](https://img-blog.csdnimg.cn/img_convert/0cac2f0d727f7a5c80efea9588f8586b.png)
sort()
对数组的元素进行排序(默认是从小到大来排序 并且是根据字符串来排序的)
参数:可选(函数) 规定排序规则 默认排序顺序为按字母升序
返回值:排序后新数组
是否改变原数组:改变
sort在不传递参数情况下,只能处理10以内(个位数)数字排序
![](https://img-blog.csdnimg.cn/img_convert/138452def023357d42e70670cce5122c.png)
![](https://img-blog.csdnimg.cn/img_convert/f0718b81559db2fc62545c5fd36e2081.png)
![](https://img-blog.csdnimg.cn/img_convert/0f220b066f55845dcaba2ec4a2fac8de.png)
![](https://img-blog.csdnimg.cn/img_convert/94cae1dcb30889bf71678292f1882f96.png)
reverse()
把数组倒过来排列
参数:无
返回值:倒序后新数组
是否改变原数组:改变
![](https://img-blog.csdnimg.cn/img_convert/70f0f24021f08a08e9012a1fdf4da722.png)
![](https://img-blog.csdnimg.cn/img_convert/407664315e371f2fefe4c9f391c8c75f.png)
forEach()
循环遍历数组每一项
参数:函数 ary.forEach(function(item,index,ary){}) item:每一项 index:索引 ary:当前数组
返回值:无
是否改变原数组:不改变
forEach中不能使用continue和break,forEach中不能跳出,只能跳过(return跳过)
![](https://img-blog.csdnimg.cn/img_convert/1e2bcdc9616fb013bdf18aaf3b77e5c5.png)
![](https://img-blog.csdnimg.cn/img_convert/52f8e8511fc58f6c05199a3dda434e74.png)
some()
some()可以检测数组中是否有某一项符合条件
![](https://img-blog.csdnimg.cn/img_convert/6a07b7ed29fbaece0e879b1e1ac6412f.png)
![](https://img-blog.csdnimg.cn/img_convert/f05314bdcf83fb59dfb6c00dfeb9944c.png)
reduce()
reduce()可以实现一个累加器的功能,将数组的每个值(从左到右)将其降低到一个值。
![](https://img-blog.csdnimg.cn/img_convert/a7cbc53c3c9c4ce82f52c542886d86b5.png)
reduceRight()
reduceRight的语法以及回调函数的规则和reduce方法是一样的,区别就是在与reduce是升序,即角标从0开始,而reduceRight是降序,即角标从arr.length-1开始。
![](https://img-blog.csdnimg.cn/img_convert/610ef7f3886dc88930bf540d9d74093f.png)
![](https://img-blog.csdnimg.cn/img_convert/7d6af24015f81a35bb9757c618ded634.png)
every()
every() 方法用于检测数组所有元素是否都符合指定条件(通过函数提供)。
every() 方法使用指定函数检测数组中的所有元素:
如果数组中检测到有一个元素不满足,则整个表达式返回 false ,且剩余的元素不会再进行检测。
如果所有元素都满足条件,则返回 true。
![](https://img-blog.csdnimg.cn/img_convert/6591b4a2e6750b000d41224101db2eb0.png)
![](https://img-blog.csdnimg.cn/img_convert/b0e22537938980a422522ac198376c7c.png)
map()
map()对数组的每一项运行给定函数,返回每次函数调用结果所组成的数组,对数组的变化不会影响到原数组
![](https://img-blog.csdnimg.cn/img_convert/e358d1bcbf3a4329ff27048d53bea199.png)
![](https://img-blog.csdnimg.cn/img_convert/383e5a8aba5525977121a47eaa968bdb.png)
filter()
filter() 对数组的每一项运行给定函数,返回该函数会返回true的项组成的数组,对数组的变化不会影响到原数组
![](https://img-blog.csdnimg.cn/img_convert/efc8f7bb353b398c7603a2ee9a1363ab.png)
![](https://img-blog.csdnimg.cn/img_convert/79a0b0b42e6b5b660d6927995b94d27e.png)
find()
find:给定条件,返回数组中第一个满足该条件的值,之后的值不再进行检测,当没有找到满足该条件的值时,返回undefined
![](https://img-blog.csdnimg.cn/img_convert/906d8b6cca7906d5be3c668bad3cfba2.png)
![](https://img-blog.csdnimg.cn/img_convert/81c0bd0eccb0a664d472b4f0b5ef8aa6.png)
findIndex()
findIndex:给定条件,返回数组中第一个满足该条件的值的下标,之后的值不再进行检测,当没有找到满足该条件的值时,返回 -1
![](https://img-blog.csdnimg.cn/img_convert/c2044a0f933c87e68af567642fc9f796.png)
![](https://img-blog.csdnimg.cn/img_convert/13fa0588d3bd95147cbaab68c74837be.png)
字符串的常用方法
charAt()
定义和用法
charAt() 方法可返回指定位置的字符。
第一个字符位置为 0, 第二个字符位置为 1,以此类推.
![](https://img-blog.csdnimg.cn/img_convert/c68b34af7eca5a07a7fa2c795e58d3a0.png)
![](https://img-blog.csdnimg.cn/img_convert/4ea2fcb8c2e87c2dfc7e6e864479d9ea.png)
charcodeAt()
定义和用法
charCodeAt() 方法可返回指定位置的字符的 Unicode 编码。
字符串中第一个字符的位置为 0, 第二个字符位置为 1,以此类推。
![](https://img-blog.csdnimg.cn/img_convert/015936045774e6cc224a0aefc11f7013.png)
![](https://img-blog.csdnimg.cn/img_convert/afe2d14a20edc7cbfd2d038d57d0672d.png)
concat
定义和用法
concat() 方法用于连接两个或多个字符串。
该方法没有改变原有字符串,但是会返回连接两个或多个字符串新字符串。
![](https://img-blog.csdnimg.cn/img_convert/48c75b0766a4fa127a7d526aeb13a71c.png)
![](https://img-blog.csdnimg.cn/img_convert/d9ccb0c803489e900f765bc98014d30a.png)
fromCharCode()
定义和用法
fromCharCode() 可接受一个指定的 Unicode 值,然后返回一个字符串。
注意:该方法是 String 的静态方法,字符串中的每个字符都由单独的 Unicode 数字编码指定。
![](https://img-blog.csdnimg.cn/img_convert/f332b952496578fe60fea2aa9b6e7616.png)
codePointAt()根据索引获取对应的字符
![](https://img-blog.csdnimg.cn/img_convert/f868302cbb9dc85f454917b679faf551.png)
![](https://img-blog.csdnimg.cn/img_convert/6ef2d432b665ac56332073c12d165b62.png)
indexOf()
定义和用法
indexOf() 方法可返回某个指定的字符串值在字符串中首次出现的位置。
如果没有找到匹配的字符串则返回 -1。
注意: indexOf() 方法区分大小写。
![](https://img-blog.csdnimg.cn/img_convert/7c86bb49814e4ba0a012737df505ceac.png)
![](https://img-blog.csdnimg.cn/img_convert/8565ccfe62696bc09ba4f17ad7b7f18b.png)
includes()
定义和用法
includes() 方法用于判断字符串是否包含指定的子字符串。
如果找到匹配的字符串则返回 true,否则返回 false。
注意: includes() 方法区分大小写。
![](https://img-blog.csdnimg.cn/img_convert/6e9bef017dc6ec3c11e31a0d439af54b.png)
![](https://img-blog.csdnimg.cn/img_convert/12f8d6a4e88346a7307ce7490e0e24be.png)
lastIndexOf()
定义和用法
lastIndexOf() 方法可返回一个指定的字符串值最后出现的位置,如果指定第二个参数 start,则在一个字符串中的指定位置从后向前搜索。
注意: 该方法将从后向前检索字符串,但返回是从起始位置 (0) 开始计算子字符串最后出现的位置。 看它是否含有字符串。
开始检索的位置在字符串的 start 处或字符串的结尾(没有指定 start 时)。
如果没有找到匹配字符串则返回 -1 。
![](https://img-blog.csdnimg.cn/img_convert/86534abedfbb8aaa74d29b48d8eaf01f.png)
![](https://img-blog.csdnimg.cn/img_convert/9ffcd490e1e693626ce3becfc1a6325f.png)
match()
定义和用法
match() 方法可在字符串内检索指定的值,或找到一个或多个正则表达式的匹配。
![](https://img-blog.csdnimg.cn/img_convert/de51269aa2c08b11d80fcfe38d472eb5.png)
![](https://img-blog.csdnimg.cn/img_convert/8b898df12539c5ed1adcf967622bd541.png)
repeat()
定义和用法
repeat() 方法字符串复制指定次数
![](https://img-blog.csdnimg.cn/img_convert/022d058b5cb797548f18224a2ae8b057.png)
![](https://img-blog.csdnimg.cn/img_convert/c654e12a85e80cb169c10aed1f05cdf7.png)
replace()
定义和用法
replace() 方法用于在字符串中用一些字符替换另一些字符,或替换一个与正则表达式匹配的子串。
该方法不会改变原始字符串。
![](https://img-blog.csdnimg.cn/img_convert/48d96d3052fbb76652f94e2129ce3cab.png)
![](https://img-blog.csdnimg.cn/img_convert/fdb450b0cacdffddd30853b13cc61734.png)
search()
定义和用法
search() 方法用于检索字符串中指定的子字符串,或检索与正则表达式相匹配的子字符串。
如果没有找到任何匹配的子串,则返回 -1。
![](https://img-blog.csdnimg.cn/img_convert/0388207814ce4ac0691a107825fd4800.png)
![](https://img-blog.csdnimg.cn/img_convert/8e8f24c8276c83fbe84d3696fa656345.png)
slice()
定义和用法
slice(start, end) 方法可提取字符串的某个部分,并以新的字符串返回被提取的部分。
使用 start(包含) 和 end(不包含) 参数来指定字符串提取的部分。
字符串中第一个字符位置为 0, 第二个字符位置为 1, 以此类推。
![](https://img-blog.csdnimg.cn/img_convert/5b7b36d61704b377fa7fc74a72800705.png)
![](https://img-blog.csdnimg.cn/img_convert/42d899bdf3fdb8809f89b075ae55b2d0.png)
split()
定义和用法
split() 方法用于把一个字符串分割成字符串数组。
提示: 如果把空字符串 ("") 用作 separator,那么 stringObject 中的每个字符之间都会被分割。
注意: split() 方法不改变原始字符串。
![](https://img-blog.csdnimg.cn/img_convert/d6ac72951d21a35d08abd300ab0874dd.png)
![](https://img-blog.csdnimg.cn/img_convert/67ec543d166742bec7dda5ec82516762.png)
startsWith()
定义和用法
startsWith() 方法用于检测字符串是否以指定的子字符串开始。
如果是以指定的子字符串开头返回 true,否则 false。
startsWith() 方法对大小写敏感
![](https://img-blog.csdnimg.cn/img_convert/01520230e600abeaaf8c930528f7c8d0.png)
![](https://img-blog.csdnimg.cn/img_convert/5d4c013465beade16b2436257ee00d45.png)
substr()
定义和用法
substr() 方法可在字符串中抽取从 开始 下标开始的指定数目的字符。
ECMAscript 没有对该方法进行标准化,因此反对使用它。
注意: substr() 方法不会改变源字符串。
![](https://img-blog.csdnimg.cn/img_convert/d220787e6d366bf838fd803a328891e7.png)
![](https://img-blog.csdnimg.cn/img_convert/df6af9c4cf0c35416b817481bd722399.png)
substring()
定义和用法
substring() 方法用于提取字符串中介于两个指定下标之间的字符。
substring() 方法返回的子串包括 开始 处的字符,但不包括 结束 处的字符。
![](https://img-blog.csdnimg.cn/img_convert/90189a7dbbf0fe2eab7d4eb7d154ca6d.png)
![](https://img-blog.csdnimg.cn/img_convert/17d76b81bbe470e69bf5917a2aae9d1c.png)
toLowerCase()
定义和用发:
把字符串转换为小写
![](https://img-blog.csdnimg.cn/img_convert/f1012452fe2f386290a9af43c00fc091.png)
![](https://img-blog.csdnimg.cn/img_convert/17388ead82f7744fe3ecab6027e28357.png)
toUpperCase()
定义和用法
把字符串转换为大写
![](https://img-blog.csdnimg.cn/img_convert/f926e5d7f5d24415a6f9c8fccfc0f904.png)
![](https://img-blog.csdnimg.cn/img_convert/5c1d9c6d712fe73630e61ac771755002.png)
trim()
定义和用法
用于删除字符串的头尾空格。
方法不会改变原始字符串。
![](https://img-blog.csdnimg.cn/img_convert/32422b4d0dc5c50b881f2d77bf6c748a.png)
![](https://img-blog.csdnimg.cn/img_convert/fefd8b0a1fdcf9546ad200bfbca5ff76.png)
valueOf()
定义和用法
valueOf() 方法可返回 String 对象的原始值
![](https://img-blog.csdnimg.cn/img_convert/deadbfe8ee365a70e86979af13b0c4ff.png)
![](https://img-blog.csdnimg.cn/img_convert/2ad5e35b432d82199aa500853de21ac0.png)
toString()
定义和用法
toString() 方法 String 对象的值。
![](https://img-blog.csdnimg.cn/img_convert/204d3eaed4a3a100ae2ff31f4eafad04.png)
toLocaleLowerCase() toLocaleUpperCase()
定义和用法
转化为本地格式的小写 转化为本地格式的大写
![](https://img-blog.csdnimg.cn/img_convert/84f0a8787d95c7cf63173bbed36c2715.png)
![](https://img-blog.csdnimg.cn/img_convert/e3488466ef15b80283b9004e589ee51a.png)
toFixed
定义和用法
数字精确小数点方法
![](https://img-blog.csdnimg.cn/img_convert/cc8cb1ee4e3dc9871fe399c7ae48ce34.png)
![](https://img-blog.csdnimg.cn/img_convert/971bb977d9e23c0ccc51aa2a8e4bbabe.png)