数组

Array.from()

Array.from()用于将可迭代对象(有Sysmbol.iterator接口对象)和类数组对象(有lengths属性对象)装换为真正的数组Array对象

Array.from(obj, func)
将类数组或者遍历对象转换为数组对象,再将每个数组元素调用func方法处理(类似map)

//lis是个类数组对象
const lis = document.querySelectorAll("li")

const liName = Array.from(lis, li => li.textContent)
console.log(liName)


function sum() {
    //arguments是类数组对象
    return Array.from(arguments).reduce((pre, curr) => pre+curr, 0)
}

Array.of()

Array.of()是为了弥补 Array(count)方法的歧义

Array(2) //[undefined, undefined]
Array(2,7) //[2, 7]

Array.of(2) //[2]
Array.of(2, 7) //[2, 7]

数组ES6新方法

  1. find()

    find(func) 参数为一个函数 返回bool 返回第一个函数返回为true的元素 找到符合条件的即会停止遍历

    const numbers = [11, 32, 24, 12]
    const result = numbers.find((element, index, array) => element == 11)
    console.log(result) // 11
  2. findIndex()

    findIndex(func) 参数为一个函数 返回bool 返回第一个函数返回为true的元素的下标值 找到符合条件的即会停止遍历

    const numbers = [11, 32, 24, 12]
    const result = numbers.find((element, index, array) => element == 11)
    console.log(result) // 0
  3. some()

    some(func) 返回bool 判断数组中是否至少有一个符合func条件 遇到符合条件的停止遍历

  4. every()

    every(func) 返回bool 判断数组中是否全部符合func条件 会遍历全部

Array常用方法

  1. slice
    arr.slice(fromIndex, toIndex)
    获取数组从开始索引到结束索引切片后的子数组

    如果结束索引大于数组长度,就获取到数组结尾停止