数组解构赋值应用
[a, b] = [b, a]
[o.a, o.b] = [o.b, o.a]
const [a, ...rest] = [...'asdf']
数组浅拷贝
const arr = [1, 2, 3]
const arrClone = [...arr]
const obj = { a: 1 }
const objClone = { ...obj }
数组合并
const arr1 = [1, 2, 3]
const arr2 = [4, 5, 6]
const arr3 = [7, 8, 9]
const arr = [...arr1, ...arr2, ...arr3]
数组去重
const arr = [1, 1, 2, 2, 3, 4, 5, 5]
const newArr = [...new Set(arr)]
数组取交集
const a = [0, 1, 2, 3, 4, 5]
const b = [3, 4, 5, 6, 7, 8]
const duplicatedValues = [...new Set(a)].filter(item => b.includes(item))
duplicatedValues
数组转对象
const arr = [1, 2, 3, 4]
const newObj = {...arr}
const obj = {0: 0, 1: 1, 2: 2, length: 3}
let newArr = [...obj]
let newArr = Array.from(obj)
数组常用遍历
检测数组所有元素是否都符合判断条件
const arr = [1, 2, 3, 4, 5]
const isAllNum = arr.every(item => typeof item === 'number')
检测数组是否有元素符合判断条件
const arr = [1, 2, 3, 4, 5]
const hasNum = arr.some(item => typeof item === 'number')