JavaScript基础常用方法(字符串/数组)

JS基础常用方法

字符串方法:

以下字符串方法,都不会改变原有的字符串

1、str.slice(n,m): 截取字符串
n 表示从该索引处开始提取字符串的字符(包括),如果为负数则从后开始计算
m 表示从该索引处结束提取字符串(不包括),如果省略则一直提取到字符串末尾,如果为负数从后开始计算

let str = 'hello world';
console.log(str.slice(6)) // 'world'
console.log(str.slice(-5,-3)) // 'wo'

2、str.substring(n,m): 同slice方法截取字符串
n 表示从该索引处开始提取字符串的字符(包括)
m 表示从该索引处结束提取字符串(不包括)
n,m 如果为负数或者NaN则都会被当做0,如果大于字符串的长度则会被当做字符串的长度来计算,如果 n 大于 m ,则 substring 的执行效果就像两个参数调换了一样

let str = 'hello world';
console.log(str.substring(6)) // 'world'
console.log(str.substring(0,5))  // 'hello'

3、str.trim(): 删除字符串两边的空格

let str = '  hello world  ';
console.log(str.trim()) // 'hello world'

4、str.replace(): 替换字符串

let str = 'hello world';
console.log(str.replace(/o/g,"f")) // "hellf wfrld"

5、str.split(): 分割字符串

let str = 'hello world';
console.log(str.split(" ")) // ["hello", "world"]
console.log(str.split("")) // ['h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd']

6、str.includes(): 判断是否包含字符串

let str = 'hello world';
console.log(str.includes('hello')) // true
console.log(str.includes('toto')) // flase

7、str.indexOf(): 判断是否包含字符串,不包含返回-1

let str = 'hello world';
console.log(str.indexOf('wor')) // 6
console.log(str.indexOf('toto')) // -1

8、str.lastIndexOf(): 用法同indexOf,lastIndexOf从后往前找
9、str.search(): 通过正则查找,找不到返回-1
10、str.match(): 通过正则查找,基本不使用
11、str.charAt(): 通过索引查字符串

let str = 'hello world';
console.log(str.charAt(0)) // 'h'

12、 str.concat(): 字符串拼接

let str = 'hello';
console.log(str.concat(' ','world')) // 'hello world'

13、str.toLowerCase(): 转小写

let str = 'ABC';
console.log(str.toLowerCase()) // 'abc'

14、str.toUpperCase(): 转大写

let str = 'abc';
console.log(str.toUpperCase()) // 'ABC'

数组方法:

1、arr.join(): 拼接成字符串 不改变原数组

let arr = [1,2,3,4,5];
console.log(arr.join('-')) // 1-2-3-4-5

2、arr.push(): 向后追加元素,返回数组长度 改变原数组
3、arr.pop(): 删除最后一个元素,返回删除的元素 改变原数组
4、arr.unshift(): 向前追加元素,返回数组长度 改变原数组
5、arr.shift(): 删除第一个元素,返回删除的元素 改变原数组

6、arr.reverse(): 翻转数组 改变原数组
7、arr.sort(): 排序数组,默认由小到大 改变原数组

let arr = [1,5,2,6,2];
console.log(arr.sort()) // [1, 2, 2, 5, 6]
arr.sort((a, b) => {
    
    
	return b - a
})
console.log(arr) // [6, 5, 2, 2, 1]

8、arr.concat(): 拼接数组 不改变原数组

let arr1 = [1,2,3];
let arr2 = [4,5,6];
console.log(arr1.concat(arr2)) // [1, 2, 3, 4, 5, 6]
console.log([...arr1, ...arr2]) // [1, 2, 3, 4, 5, 6]
arr1.push(...arr2)
console.log(arr1) // [1, 2, 3, 4, 5, 6]

9、arr.slice(): 截取数组,前包后不包 不改变原数组

let arr = [0,1,2,3,4,5,6,7];
console.log(arr.slice(0,3)) // [0, 1, 2]
console.log(arr.slice(3)) // [3, 4, 5, 6, 7]

10、arr.splice(a,b,c): 删除/替换/添加数组 改变原数组
a开始
b删除几个
c添加元素

let arr = [1,2,3,4,5];
arr.splice(2,1,6,6,6)
console.log(arr) // [1, 2, 6, 6, 6,  4, 5]

11、arr.indexOf(): 查找元素在数组中第一次出现的位置,-1不存在
12、arr.lastIndexOf(): 查找元素最后一次在数组中出现的位置,-1不存在
13、arr.forEach(item,index,array): 正常遍历,无返回值
14、arr.map(item,index,array): 返回一个新数组,新数组的长度和原数组长度相等
15、arr.filter(item,index,array): 返回一个新数组, 过滤出符合条件的元素
16、arr.some(item,index,array): 返回true/false(一个都不符合返回false)
17、arr.every(item,index,array): 返回true/false(全部满足返回true)
18、arr.every(item,index,array): 返回符合条件第一项
19、arr.findIndex(item,index,array): 返回符合条件第一项下标

20、arr.reduce(pre,next,index): 累计循环
pre 上一项返回值(第一次为arr第一项)
next 普通循环的item
index index

// 加法计算
let arr = [1,2,3,4,5] ;
let all = arr1.reduce(function(pre,next,index){
    
    
	return pre+next
})
console.log(all) // 21

//扁平化数组
var arr2 = [[1,2,3],[4,5],[6,7]] ;
var new2 = arr2.reduce(function(pre,next,index){
    
    
	return pre.concat(next)
})
console.log(new2);

猜你喜欢

转载自blog.csdn.net/qq_41329287/article/details/129285219