ES6-3

    window.onload = function () {
        // ---------------------------------------数组解构-----------------
        let [x, y, z] = [true, [1, 2, 3], { id: 1 }];
        //等价
        // x=true;
        // y=[1,2,3];
        // z={id:1};
        let arr = [10, 20];
        if (arr[0]) {
            let x2 = arr[0]
        } else {
            let x2 = 1;
        }
        //简写
        let x2 = arr[0] || 1;
        //es6
        let [x3 = 1, x4 = 1] = arr;

        let [xx3 = 1, xxx4 = 1] = [undefined, null];   //xxx3=1,xxx4=null   undefined默认值生效  
        //对象解构

        let obj1 = {
            id: 999,
            name: 'kk',
            data: {
                name: 'bb',
                title: 'tt'
            },
            res: {
                data: {
                    act: '888act',
                    func: '999func'
                }
            }
        }
        // let id = obj1.id;
        // let act = obj1.res.data.act;
        //等价于  ES6
        let { id: _id = 1, act: _act = 'actdefault' } = obj1;     //:改的别名 = 给的默认值  es6更方便
        console.log(obj1);
        console.log(_id);
        //--------------------------------------------------------------------------------------------------------
        let arrk = [1, 2, 3, 4, 5, 6, 7, 1, 2, 5, 6, 7, 9, 10];
        const arrs = new Set();
        arrs.add(1).add(2).add(3).add(2); console.log(arrs);  //  {1,2,3}
        arrs.add(1).add(2).add(3).add('2'); console.log(arrs);  //  {1,2,3,'2'}  进行的是类型匹配

        const arr3 = new Set();
        arr3.add(arrk);
        console.log(arr3);    //这样没去重,相当于把arrk 作为一个对象添加进去了

        var arrs4 = new Set(arrk); console.log(arrs4);    //去重后的arrk   但不是数组了

        arrk.forEach((i) => {
            arrs.add(i);
        })
        console.log(arrs);

        let obj3 = new Object();
        obj3.id = 1;
        obj3['msg'] = 'no data';    //这添加的是属性
        //   obj3[msg] = 'nodata';       //msg 这只是一个变量  

        Array.from(arrs4);  //{}对象转数组
        [...arrs4];   //{}对象转数组


        // Array.from(arrs4((i) => {
        //     i > 5
        // }));

        console.log("//////////////////")
        console.log(Array.from(new Set(arrk.filter(i => i > 5))))    //去重排序

        const arr6 = [{ value: 30 }, { value: 10 }, { value: 3 }, { value: 60 }, { value: 30 }, { value: 30 }, { value: 30 }, { value: 30 },]  //去掉含3的

        let kk = arr6.filter(i => (i.value / 3 == 0));
        console.log([...(new Set(arr6.filter(i => ((i.value / 3) != 0))))])
    }

 

posted @ 2022-09-19 11:45  wolfsocket  阅读(19)  评论(0编辑  收藏  举报