avatar

目录
常用的数组字符串方法
  • []是可选参数的意思
  • <>是必填参数的意思

数组

数组的增

arr.push()

添加到数组的最后面,会修改原数组,并且返回数组的长度

arr.unshift()

添加到数组的最前面,会修改原数组,并且返回数组的长度

数组的查

arr.filter(function(item,[index],[arr])>,[thisValue])

filter() 方法创建一个新的数组,新数组中的元素是通过检查指定数组中符合条件的所有元素。

Code
1
2
3
4
5
6
var ages = [32, 33, 16, 40];

let aa = (age)=>{
return age >= 18;
}
console.log(aa()) // 32,33,40

arr.every()

是对数组中每一项运行给定函数,如果该函数对每一项返回true,则返回true。

arr.some()

是对数组中每一项运行给定函数,如果该函数对任一项返回true,则返回true。

arr.map(function(item,[index],[arr]),[thisValue])

map() 方法返回一个新数组,数组中的元素为原始数组元素调用函数处理后的值。

arr.find()和arr.findIndex

ES6,此方法返回元素第一个符合条件的值
Code
1
2
3
4
5
var ages = [3, 10, 18, 20];

ages.find((item)=> return age >= 16); // 18

age.findIndex((item) => return item >= 16); // 2

arr.includes(str)

检测数据是否包含str

Code
1
2
3
4
5
6
7
let site = ['runoob', 'google', 'taobao'];

site.includes('runoob');
// true

site.includes('baidu');
// false

数组的改

arr.join(str)

将数组转换为字符串,不会修改原字符串,返回一个新的字符串,不传参数默认,

arr.sort(sortfunction)

将数组的元素进行排序,默认是字母排序,使用数字排序时,必须用过一个函数作为参数来调用

字符串数组:

Code
1
2
let fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.sort(); // Apple,Banana,Mango,Orange

数字数组:

Code
1
2
let points = [40,100,1,5,25,10];
points.sort((a,b)=>{return a-b}); // 1,5,10,25,40,100

arr.reverse()

将数组中的元素颠倒排序,会修改原来的数组

arr.concat(str)

将数组与后面的字符串拼接成一个新数组,并且返回新数组,不会修改原数组

Code
1
2
let [a,b] = [[1,2,3],[4,5,6]];
console.log(a.concat(...b)); // 1,2,3,4,5,6

arr.slice([start],[end])

将数组按照start和end截取字符串,不会修改原数组

Code
1
2
var fruits = ["Banana", "Orange", "Lemon", "Apple", "Mango"];
var citrus = fruits.slice(1,3); // Orange,Lemon

fill(value,[start],[end])

ES6,此方法将一个数组的值替换成value,从start开始end结束

Code
1
2
var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.fill("Runoob");

数组的删

arr.splice(index,[quantity],[item1],[item2],…..)

用于添加或删除数组中的元素

从第index个删除,删除quantity个,会改变原来的数组,并且返回当前被删除的元素

删除:

Code
1
2
var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.splice(2,1); // Banana,Orange,Mango

添加:

Code
1
2
var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.splice(2,0,"Lemon","Kiwi"); //Banana,Orange,Lemon,Kiwi,Apple,Mango

arr.pop()

删除数组的最后一个元素并返回,会修改元素组

Code
1
2
var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.pop(); // Banana,Orange,Apple

arr.shift()

删除数组的第一个元素并返回,会修改原数组

Code
1
2
var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.shift()

Array.from()

ES6的方法,用于将伪数组转换成真数组

Code
1
2
const lists = document.getElementsByTagName("li");
const arrayLists = Array.from(lists);

ES6以前用的方法

Code
1
2
const lists = document.getElementsByTagName("li");
const arrayLists = Array.prototype.slice.call(lists)

字符串

字符串的增

concat()

连接两个或多个字符串,返回连接后的字符串

Code
1
2
3
let strArr = ["hello","wrold"];
let str = "hey";
console.log(str.concat(...strArr))

repeat()

ES6:repeat返回一个新字符串,表示原字符串重复n次

Code
1
2
3
4
5
'x'.repeat(3) // "xxx"
'hello'.repeat(2) // "hellohello"
'na'.repeat(0) // ""

na'.repeat(2.9) // "nana"

padStart(),padEnd()

ES2017:引入了字符串不全长度的功能,如果某个字符串的不够指定长度,会在头部或者尾部不全

  • padStart()用于补全头部
  • padEnd()用于补全尾部
Code
1
2
3
4
5
x'.padStart(5, 'ab') // 'ababx'
'x'.padStart(4, 'ab') // 'abax'

'x'.padEnd(5, 'ab') // 'xabab'
'x'.padEnd(4, 'ab') // 'xaba'

如果省略第二个参数默认使用空格补全长度

Code
1
2
'x'.padStart(4) // '   x'
'x'.padEnd(4) // 'x '

字符串的改x

fromCharCode()

将Unicode转换为字符串

replace()

替换与正则表达式相匹配的值

slice()

提取字符串的片段,并在新的字符串中返回被提取的部分

Code
1
2
const sayHello = "hello world";
console.log(sayHello.slice(0,5)) // hello

split()

把字符串分割成数组

Code
1
2
const sayHello = "hello world";
console.log(sayHello.split(" ")); // ["hello","world"]

substr()

从起始索引号提取字符串中指定数目的字符

Code
1
2
const sayHello = "hello world";
console.log(sayHello.substr(0,5)); // hello

toLocaleLowerCase()和toLowerCase()

  • toLocaleLowerCase:把特定语言把英文转换为小写

  • toLowerCase:把字符串转换成小写

Code
1
2
const sayHello = "HELLO WORLD";
console.log(sayHello.toLowerCase()); // hello world

toLocaleUpperCase()和toUpperCase

  • toLocaleLowerCase:把特定语言把英文转换为大写
  • toUpperCase:把字符串转换为大写
Code
1
2
const sayHello = "hello world";
console.log(sayHello.toUpperCase()); // HELLO WORLD

toString()

返回字符串对象值

valueOf()

返回某个字符串对象的原始值

raw()

ES6为原生的String对象,提供了一个raw()方法。该方法返回的一个斜杠都被转义

javascript
1
2
String.raw`Hi\\n`
// 返回 "Hi\\\\n"

trimStart(),trimEnd()

ES2019对字符串实例新增了这两个方法。他们的行为与trim()一致

  • trimStart() 返回新的字符串,消除字符串头部的空格
  • trimEnd() 返回新的字符串,消除字符串尾部的空格
Code
1
2
3
4
5
const s = '  abc  ';

s.trim() // "abc"
s.trimStart() // "abc "
s.trimEnd() // " abc"

字符串的删

trim()

移除字符串首尾空白

字符串的查

charAt()

返回指定索引位置的字符串

charCodeAt()

返回指定索引位置字符串的Unicode值

indexOf()

返回字符串中检索指定字符第一次出现的位置

lastIndexOf()

返回字符串中检索指定字符最后一次出现的位置

match()

找到一个或多个正则表达式匹配的子串

检索于正则表达式相匹配的值

split(str)

那字符串转换成数组

localeCompare()

用本地特定的顺序来比较两个字符串

includes(),startsWith(),endsWith()

在ES6以前我们可以使用indexOf()来判断一个字符串是否包含在另一个字符串中。在ES6又提供了三种新方法

  • includes() 返回布尔值,表示是否找到了参数字符串
  • startsWith() 返回布尔值,表示参数字符串是否在原字符串的头部
  • endsWith() 返回布尔值,表示参数字符串是否在原字符串的尾部
javascript
1
2
3
4
5
let s = 'Hello world!';

s.startsWith('Hello') // true
s.endsWith('!') // true
s.includes('o') // true

这三个方法都支持第二个参数,表示开始搜索的位置

javascript
1
2
3
4
5
let s = 'Hello world!';

s.startsWith('world', 6) // true
s.endsWith('Hello', 5) // true
s.includes('Hello', 6) // false
文章作者: 青空
文章链接: https://gitee.com/AIR-ZRB/blog/2020/03/16/JavaScript-%E5%B8%B8%E7%94%A8%E6%95%B0%E7%BB%84%E5%AD%97%E7%AC%A6%E4%B8%B2/
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 青空
打赏
  • 微信
    微信
  • 支付寶
    支付寶