小小的js

    //安全登陆不允许iframe嵌入 
    if (window.top !== window.self) {
        window.top.location = window.location;
    }

使用filter过滤参数

    let a = [0];
    a = a.filter(item => {
        return item
    });

    console.log(a); //[]  0也会被过滤掉

    let b = [0,1,2,undefined];
    b = b.filter(item => {
        return item
    });

    console.log(b);//[1, 2]

reduce谜之操作

    const arr = [{id: 1,type: 'A',total: 3}, {id: 2,type: 'B',total: 5}, {id: 3,type: 'E',total: 7}];
    
    const re = arr.reduce((sum,{total})=>{
        return sum + total;
    },0);

    const re1 = arr.reduce((str, { id, type,total }) => {
        return str + `id:${id},type:${type},total:${total};`;
    }, '');

    const re2 = arr.reduce((res, { id, type, total }) => {
        res[id] = {
            type,
            total
        };
        return res;
    }, {});

    const re3 = arr.reduce((res, { id, type, total },index) => {
        res[index] = type;
        return res;
    }, []);

console.log(re); //结果为total的累加
console.log(re1); //id:1,type:A,total:3;id:2,type:B,total:5;id:3,type:E,total:7;字符串
console.log(re2); //{1: {type: "A", total: 3}, 2: {…}, 3: {…}}
console.log(re3); //["A", "B", "E"]

const re3 = arr.reduce((
第一个参数:累加器累加回调的返回值-res,
第二个参数:数组中正在处理的元素-{ id, type, total },
第三个参数可选:当前元素的索引-index,
第四个参数可选:调用reduce()的数组-array) => {
        res[index] = type;
        return res;
    }, []);
posted @ 2018-10-09 15:02  hid3onbush  阅读(155)  评论(0编辑  收藏  举报