ES6数组新增

//Array.from(arr) 把类数组(获取的彝族元素,argument...)对象转换成数组
    //类数组就是具备length的东西 都是可以使用Array.from()来进行转换的
    let str = "zeng!!!!!"
    //let s = str.split("") 切分成数组也是可以的

    let s = Array.from(str)
    console.log(s)
console.log("-------------------------")

    //Array.of() 把一组值转换成数组 这样就把 字符串转换成数组了
    let arr1 = Array.of(str)
    console.log(arr1)
    console.log("============================")
    //arr.find() 查找数组成员第一个出现的 的成员
    let arr2 = [2, 44, 56, 78, 89, 344, 6777]

    let res = arr2.find((item, index, arr2) => {
        return item > 500 //这边就是打印出 大于500第一个出现的成员
    })
    console.log(res)
    console.log("-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-")
    //arr.findindex() 查找索引
    let rec = arr2.findIndex((item, index, arr2) => {
        return item > 300 //查找值大于300 且第一次出现的 的索引
    })
    console.log(rec)

    console.log("[][][][][[[[[[[[[[]]]]]]]]]]]]]]")
    //arr.fill(填充的东西,开始的位置,结束的位置)
    let arr4 = [1,2,3,4,5,6,7,8,9]
    arr4.fill("默认值的数据", 1, 3) //包头不包含尾巴 就是给定索引位置的 元素替换掉
    console.log(arr4)

    //arr.includes() 数组里面是否包含 有包含则返回true 
    let arr5 = ["apple","banana","orange"]
    let ab = arr5.includes("apple") //数组里面是否包含某个数据
    console.log(ab)

    console.log("------------------------------------")
    //小练习
    //数组是复合的数据类型,直接复制的话,只是复制了指向底层数据结构的指针,
        //而不是克隆一个全新的数组。
    const a1 = [1,2,66666];
    const a2 = a1
    console.log(a2)
    // console.log(a1 = a2)
    console.log("-=================================")
    // 成员都是对原数组成员的引用,这就是浅拷贝 下面的方法就是浅拷贝
    const aa1 = [{ foo: 1 }]
    const aa2 = [{ bar: 2 }]
    const a3 = aa1.concat(aa2)  //复制一份 es5
    const a4 = [...aa1,...aa2]
    console.log(a3)
    console.log(a4)

    console.log("===============================================================================================")
/**
 * @description: 2019/4/12 数组新增
 * @param : 
 * @return: 
 */    
    /*1.数组扩展运算符: 就是可以展开数组为逗号隔开的*/
    //扩展运算符(spread)是三个点(...)。它好比 rest 参数的逆运算,将一个数组转为用逗号分隔的参数序列。
    let fgc = ["1","2","3","4"]
    console.log(...fgc)
    //逆向小栗子 rest
    // function dd(aa,...bb){
    //     console.log(aa)
    //     console.log(bb) //[555]
    // }
    // dd([1,2,3,4,5],555)

    //扩展运算符在函数的运用
    let arraykk = []
    function pushs(x,y,z,k,l){
        console.log(x+y+z+k+l)
    }
    const pi = [333,444,555]
    pushs(...pi,"你好","hello-world") //这边就是变成 一个参数序列

    //替代函数的apply方法 this指向的方法
    console.log(Math.max.apply(null, [14, 3, 77]))// 77 es5
    console.log(Math.max(...[14,3,77])) //77 es6

    let fff1 = [1,2,3,4]
    let fff2 = [5,6,7,8]
    fff1.push(...fff2)
    console.log(fff1)    //(8) [1, 2, 3, 4, 5, 6, 7, 8]

    console.log(new Date(...[2015,1,1])) //Sun Feb 01 2015 00:00:00 GMT+0800 (中国标准时间)

    //用来复制数组 下面是错误的示范 
    const gh1 = [11,22]
    const gh2 = gh1 //这边只是改变了指向底层数据结构的指针,而不是克隆了一个全新的数组
    gh2[0] = 33 //使用concat就行
    console.log(gh1) //[33, 22]

    //使用扩展运算符 复制数组
    const gh3 = [555,666]
    const gh4 = [...gh3]
    console.log(gh3) //[555, 666]这边就成功的复制了一个数组
    //合并数组
    const gh5 = [1,2,3]
    const gh6 = [11,22,33]
    const gh7 = [111,222,333]
    console.log(...gh5,...gh6,...gh7)//1 2 3 11 22 33 111 222 333 合并数组
    //结构赋值的结合
    const [first,...rest] = ["ZENG"]
    console.log(first)
    console.log(rest)
    //扩展运算符 还可以用于字符串
    console.log(..."hello")

    /*2.Array.from: 将有length属性的 转成数组*/
    let gh8 = "234234hws"
    console.log(Array.from(gh8)) // ["2", "3", "4", "2", "3", "4", "h", "w", "s"]

    /*3.Array.of: 用于将一组值转换成数组*/
    console.log(Array.of(2,3,4,5,88)) //[2, 3, 4, 5, 88]

    /*4.数组的find()和findIndex()方法*/
    let cf = ['www','csd','fd','srsd']
    console.log(cf.find(function(value,index,cf){
        return value > 1
    }))

 

你可能感兴趣的:(ES6数组新增)