1. 扩展运算符
应用
1.复制数组
const arr = [1, 3, 5]
const arr2 = [2, 4, 6]
(1)es5
const res = arr.concat()
console.log(res)
const res2 = [...arr]
console.log(res2)
2.合并数组
const res = arr.concat(arr2)
console.log(res)
const res2 = [...arr, ...arr2]
console.log(res2)
3.与解构赋值混用
const [first, ...res] = [1, 2, 3, 4]
console.log(first)
console.log(res)
const [second, ...res] = []
console.log(second)
console.log(res) = []
const [third, ...rest] = ['foo']
console.log(third)
console.log(...rest)
4.字符串
const str = 'hello'
const resStr = [...str]
console.log(resStr)
(1)参数1 想要转化的参数
const res = Array.form([1, 2, 3])
console.log(res)
const res2 = Array.form('hello')
console.log(res2)
let arrayLike = {
'0': 'a',
'1': 'b',
'2': 'c',
length: 3
}
const res3 = Array.form(...arrayLike)
console.log(res3)
(2)参数2 ,可以相当于map、filter等函数,对参数1进行处理
const res4 = Array.form([1, 2, 3], x => x + 1)
console.log(res4)
(3)参数3 如果map, filter里面有this,参数3可改变this指向
3. Array.of() 用于将一组数值转化为数组
(1)构造函数
const arr = new Array()
const arr2 = new Array(3)
const arr3 = new Array(3, 8, 11)
const arr4 = new Array(undefined)
(2)Array.of()
const ar = Array.of()
const ar2 = Array.of(3)
const ar3 = Array.of(3, 8, 11)
const ar4 = Array.of(undefined)
4. Object.keys()、values()、entries() 分别是对键名、键值、键值对的遍历
const obj = {
name: "slm",
age: 11,
gender: '男'
}
for(const key of obj.keys()){
console.log(key)
}
for(const key of obj.values()){
console.log(key)
}
for(const key of obj.entries()){
console.log(key)
}
5. Array.includes() 表示某个数组是否包含给定的值,回一个布尔值
const arr = [1, 3, 5]
const res = arr.includes(3)
console.log(res)
6. Array.at() 找到索引对应的值
const arr = [1, 2, 3, 4, 6]
const res = arr.at(2)
console.log(res)
const res2 = arr.at(-1)
console.log(res2)
const res3 = arr.at(100)
console.log(res3)
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?