小小的js

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

使用filter过滤参数

<script>
    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]
</script>

reduce谜之操作

<script>
    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;
    }, []);
</script>

猜你喜欢

转载自www.cnblogs.com/hideonbush/p/9760349.html