数组截取数据方法
arr.slice(start, end)
start: 开始截取的索引(包含)
end: 结束截取的索引(不包含)
作用: 返回一个新数组,包含从开始到结束(不包括结束)的元素
const arr3 = [1, 2, 3, 4, 5]
const newArr = arr3.slice(1, 4)
console.log(newArr)// [2, 3, 4]
arr.splice(start, deleteCount, item1, item2, ...)
start: 开始修改的索引
deleteCount: 要删除的元素个数
item1, item2, ...: 要添加到数组的元素
作用: 删除或替换数组中的元素,并返回被删除的元素
const arr3 = [1, 2, 3, 4, 5]
const newArr = arr3.splice(1, 2)
console.log(newArr)// [2, 3]
console.log(arr3)//[1, 4, 5]
array.filter(callback)
callback: 测试函数,返回 true 或 false
作用: 创建一个新数组,包含通过测试函数的所有元素
const arr3 = [1, 2, 3, 4, 5]
const newArr = arr3.filter(item => item > 2)
console.log(newArr)// [3, 4, 5]
console.log(arr3)//[1, 2, 3, 4, 5]
对象截取数据方法
对象解构赋值 const { prop1, prop2 } = obj
作用: 从对象中提取属性并赋值给变量
const obj = { a: 1, b: 2, c: 3 }
const { a, b } = obj
console.log(a) // 1
console.log(b) // 2
Object.keys(obj)
作用: 返回一个包含对象所有可枚举属性的数组
返回值: 属性名数组
const obj = { a: 1, b: 2, c: 3 }
const keys = Object.keys(obj) // ['a', 'b', 'c']
console.log(keys)
Object.values(obj)
作用: 返回一个包含对象所有可枚举属性值的数组
返回值: 属性值数组
const obj = { a: 1, b: 2, c: 3 }
const values = Object.values(obj)
console.log(values) //[1,2,3]
Object.entries(obj)
作用: 返回一个包含对象所有可枚举属性的键值对数组
返回值: 键值对数组
const obj = { a: 1, b: 2, c: 3 }
const entries = Object.entries(obj) // [['a', 1], ['b', 2], ['c', 3]]
console.log(entries)
Object.assign(target, ...sources)
作用: 将一个或多个源对象的属性复制到目标对象
返回值: 目标对象
const obj1 = { a: 1 }
const obj2 = { b: 2 }
const newObj = Object.assign({}, obj1, obj2) // { a: 1, b: 2 }
console.log(newObj)
字符串截取方法
str.slice(startIndex, endIndex)
startIndex: 开始截取的索引(包含)
endIndex: 结束截取的索引(不包含)。如果省略,则截取到字符串末尾
返回值: 新的字符串
作用: 提取字符串的一部分,并返回一个新的字符串
const str = "Hello, World!"
const newStr = str.slice(0, 5)// "Hello"
const newStr2 = str.slice(7) // "World!"
console.log(newStr)
console.log(newStr2)
str.substring(startIndex, endIndex)
startIndex: 开始截取的索引(包含)
endIndex: 结束截取的索引(不包含)。如果省略,则截取到字符串末尾
注意:
如果 startIndex > endIndex,substring() 会自动交换两个参数
不支持负数索引
返回值: 新的字符串
作用: 提取字符串的一部分,并返回一个新的字符串
const str = "Hello, World!"
const newStr = str.substring(0, 5)// Hello
const newStr2 = str.substring(7) // World!
console.log(newStr)
console.log(newStr2)
str.substr(startIndex, length) 已弃用
startIndex: 开始截取的索引
length: 截取的长度。如果省略,则截取到字符串末尾
注意:
substr() 是非标准方法,已弃用,建议使用 slice() 或 substring()
返回值: 新的字符串
作用: 从指定位置开始截取指定长度的字符串
const str = "Hello, World!"
const newStr = str.substr(0, 5) // "Hello"
const newStr2 = str.substr(7) // "World!"
console.log(newStr)
console.log(newStr2)
str.split(separator, limit)
separator: 分隔符(可以是字符串或正则表达式)
limit: 返回数组的最大长度
返回值: 拆分后的数组
作用: 将字符串按指定分隔符拆分为数组,然后可以截取部分内容
const str = "Hello, World!"
const arr4 = str.split(" ") // ['Hello,', 'World!']
const firstWord = arr4[0] // Hello,
console.log(arr4)
console.log(firstWord)
str.charAt(index)
index: 字符的索引
返回值: 指定位置的字符
作用: 返回字符串中指定位置的字符
const str = "Hello, World!"
const char = str.charAt(1)// "e"
console.log(char)
str.charCodeAt(index)
index: 字符的索引
返回值: Unicode 编码
作用: 返回字符串中指定位置的字符的 Unicode 编码
const str = "Hello"
const code = str.charCodeAt(1) // 101
console.log(code)
str.match(regexp)
regexp: 正则表达式
返回值: 匹配结果的数组,如果没有匹配则返回 null
作用: 使用正则表达式匹配字符串,返回匹配结果
const str = "Hello, World!"
const matches = str.match(/o/g) // ["o", "o"]
str.replace(searchValue, replaceValue)
searchValue: 要查找的内容(可以是字符串或正则表达式)
replaceValue: 替换的内容
返回值: 新的字符串
作用: 替换字符串中的部分内容
const str = "Hello, World!"
const newStr = str.replace("World", "JavaScript") // "Hello, JavaScript!"
str.trim()
返回值: 新的字符串
作用: 去除字符串两端的空白字符
const str = " Hello, World! "
const newStr = str.trim() // "Hello, World!"
str.padStart(targetLength, padString)
str.padEnd(targetLength, padString)
targetLength: 目标长度
padString: 填充的字符(默认为空格)
返回值: 新的字符串
作用: 在字符串的开头或结尾填充指定字符,直到字符串达到指定长度
const str = "Hello"
const paddedStart = str.padStart(10, "*") // "*****Hello"
const paddedEnd = str.padEnd(10, "*") // "Hello*****"