字符串常用方法:
1、charAt():返回指定索引位置的字符;
let str = 'Hello, world!'
let char = str.charAt(1)
console.log(char) // 输出 "e"
2、indexOf():返回指定字符串在原字符串中第一次出现的位置,不存在则返回-1;
let str = 'Hello, world!'
let index = str.indexOf('world')
console.log(index) // 输出 7
3、lastIndexOf():返回指定字符串在原字符串中最后一次出现的位置,不存在则返回-1;
let str = 'Hello, world!'
let index = str.lastIndexOf('o')
console.log(index) // 输出 8
4、replace():返回一个新字符串,其中所有匹配的子字符串都被替换为指定的新字符串;
let str = 'Hello, world!'
let newStr = str.replace('world', 'JavaScript')
console.log(newStr) // 输出 "Hello, JavaScript!"
5、substring():返回一个新字符串,包含从指定索引处开始到结束位置(不包括结束位置)的字符;
let str = 'Hello, world!'
let subStr = str.substring(7)
console.log(subStr) // 输出 "world!"
6、toLowerCase():返回一个新字符串,所有大写字符转换为小写字符;
let str = 'Hello, world!'
let lowerStr = str.toLowerCase()
console.log(lowerStr) // 输出 "hello, world!"
7、toUpperCase():返回一个新字符串,所有小写字符转换为大写字符;
let str = 'Hello, world!'
let upperStr = str.toUpperCase()
console.log(upperStr) // 输出 "HELLO, WORLD!"
8、split():将一个字符串分割为字符串数组,并返回该数组;
let str = 'Hello'
let arr = str.split(',')
console.log(arr) // 输出 ["H","e","l","l","o"]
9、slice(start,end):提取字符串中的一部分,并返回新的字符串(不包括end部分);
let str = 'Hello, world!'
let subStr = str.slice(7, 12)
console.log(subStr) // 输出 "world"
10、substr(start,length):从字符串中提取从start开始的指定长度length的部分,并返回一个新字符串。start 和length都是必需的参数。
let str = 'Hello, world!'
let subStr = str.substr(7, 5)
console.log(subStr) // 输出 "world"
11、concat():将两个或多个字符串连接起来,并返回新字符串;
let str1 = 'Hello, '
let str2 = 'world!'
let newStr = str1.concat(str2)
console.log(newStr) // 输出 "Hello, world!"
12、endsWith():判断一个字符串是否以指定的字符结尾,返回布尔值;
let str = 'Hello, world!'
let endsWith = str.endsWith('!')
console.log(endsWith) // 输出 true
13、startsWith():判断一个字符串是否以指定的字符开头,返回布尔值;
let str = 'Hello, world!'
let startsWith= str.startsWith('Hello')
console.log(startsWith) // 输出 true
14、includes():判断一个字符串是否包含指定字符串,返回布尔值;
let str = 'Hello, world!'
let includes = str.includes('world')
console.log(includes) // 输出 true
数组常用方法:
1、push():向数组末尾添加一个或多个元素;
const arr = [1, 2, 3]
arr.push(4)
console.log(arr) // 输出 [1, 2, 3, 4]
2、pop():从数组末尾删除一个元素,并返回该元素的值;
const arr = [1, 2, 3]
const lastItem = arr.pop()
console.log(lastItem) // 输出 3
console.log(arr) // 输出 [1, 2]
3、shift():从数组开头删除一个元素,并返回该元素的值;
const arr = [1, 2, 3]
const firstItem = arr.shift()
console.log(firstItem) // 输出 1
console.log(arr) // 输出 [2, 3]
4、unshift():向数组开头添加一个或多个元素;
const arr = [1, 2, 3]
arr.unshift(0)
console.log(arr) // 输出 [0, 1, 2, 3]
5、splice():在数组中添加或删除元素,或者替换元素;
let fruits = ['apple', 'banana', 'orange', 'grape'];
// 从数组中删除一个元素
fruits.splice(2, 1); // 从索引为 2 的位置删除 1 个元素
console.log(fruits); // 输出: ["apple", "banana", "grape"]
// 在数组中插入一个元素
fruits.splice(1, 0, 'kiwi'); // 在索引为 1 的位置插入 'kiwi' 元素
console.log(fruits); // 输出: ["apple", "kiwi", "banana", "grape"]
// 替换数组中的元素
fruits.splice(2, 1, 'mango', 'peach'); // 从索引为 2 的位置删除 1 个元素,插入 'mango' 和 'peach' 元素
console.log(fruits); // 输出: ["apple", "kiwi", "mango", "peach", "grape"]
6、slice():截取数组的一部分,并返回新数组,不会改变原数组;
const arr = [1, 2, 3, 4, 5]
const newArr = arr.slice(1, 4)
console.log(newArr) // 输出 [2, 3, 4]
console.log(arr) // 输出 [1, 2, 3, 4, 5]
7、concat():将两个或多个数组合并为一个新数组;
const arr1 = [1, 2, 3]
const arr2 = [4, 5, 6]
const newArr = arr1.concat(arr2)
console.log(newArr) // 输出 [1, 2, 3, 4, 5, 6]
8、forEach():遍历数组每一项,并执行所提供的函数;
const arr = [1, 2, 3]
arr.forEach((item, index, array)=> {
console.log(item, index, array)
})
// 输出
// 1 0 [1, 2, 3]
// 2 1 [1, 2, 3]
// 3 2 [1, 2, 3]
9、map():遍历数组每一项,并执行所提供的函数,与forEach的区别是会返回一个新数组;
let arr = [1, 2, 3];
let newArr = arr.map(item => item * 2);
console.log(newArr); // 输出: [2, 4, 6]
10、filter():遍历数组每一项,返回一个符合条件的新数组;
let arr = [1, 2, 3, 4, 5];
let newArr = arr.filter(item => item % 2 === 0);
console.log(newArr); // 输出: [2, 4]
11、reverse():用于颠倒数组中元素的顺序,会改变原数组;
let arr = [1, 2, 3];
arr.reverse();
console.log(arr); // 输出: [3, 2, 1]
12、join():将数组合并为一个字符串,可指定字符分隔,默认",",返回这个字符串;
let arr = ['apple', 'banana', 'orange'];
let str = arr.join('-');
console.log(str); // 输出: "apple-banana-orange"
13、sort():用于对数组元素排序,会改变原数组,如果不传递参数,将默认按UniCode编码排序;
let arr = [4, 2, 5, 1, 3];
arr.sort((a, b) => a - b); // 升序排列
console.log(arr); // 输出: [1, 2, 3, 4, 5]
14、reduce():对数组中的每个元素执行一个指定的操作,并将操作的结果累积到一个最终值中。语法如下:
array.reduce(callback(accumulator, currentValue, index, array), initialValue)
15、some():判断数组中是否有满足条件的元素:
const numbers = [1, 2, 3, 4, 5];
const evenExists = numbers.some(item=>item===2);
console.log(evenExists); // 输出: true
其中,callback是回调,它接收4个参数:
accumulator
:累计器,用于存储累积的结果。初始值可以通过 initialValue
参数设置,如果没有设置初始值,则默认使用数组中的第一个元素作为初始值。
currentValue
:当前元素的值。
index
:当前元素的索引。
array
:原始数组对象。
常用例子:
// 计算数组中所有元素的和
const numbers = [1, 2, 3, 4, 5];
const sum = numbers.reduce((accumulator, currentValue) => accumulator + currentValue);
console.log(sum); // 15
// 将二维数组转化为一维数组
const matrix = [[1, 2], [3, 4], [5, 6]];
const flattened = matrix.reduce((accumulator, currentValue) => accumulator.concat(currentValue));
console.log(flattened); // [1, 2, 3, 4, 5, 6]
// 统计数组中每个元素的出现次数
const fruits = ['apple', 'banana', 'orange', 'apple', 'banana', 'apple'];
const count = fruits.reduce((accumulator, currentValue) => {
if (!accumulator[currentValue]) {
accumulator[currentValue] = 1;
} else {
accumulator[currentValue]++;
}
return accumulator;
}, {});
console.log(count); // { apple: 3, banana: 2, orange: 1 }