zoukankan      html  css  js  c++  java
  • JS小技巧

    一些资料上看到的技巧,感觉挺实用,以后遇到还会继续增加

    1、删除数组尾部元素
    
    const arr = [0,1,2,3,4,5]
    
    arr.length =3;
    conosle.log(arr)// => [0,1,2]
    
    arr.length = 0;
    console.log(arr)// => []
    console.log(arr[2]) // => undefined
    
    2、数组去重
    
    const remove = arr = [...new Set(arr)];
    
    remove([42,'foo',true,true]);
    
    // =>[42,'foo',true]
    
    3、平铺多维数组
    
    const arr = [11,[22,33],[44,55],66];
    const flatArr = [].concat(...arr); // [11,22,33,44,55,66]
    
    上面的方法只适用于二维数组;需要通过递归可以平铺任意维度的嵌套数组:
    
    function flattenArray(arr){
    
        const flattened = [].concat(...arr);
        return flattened.some(item=>Array.isArray(item)) ? flattenArray(flattened) : flattened;
    
    }
    
    const arr = [11,[22,33],[44,[55,66,[77,[88]],99]]];
    
    const flatArr = flattenedArray(arr);
    
    // => [11,22,33,44,55,66,77,88,99]
    
    4、删除数组中某一个属性
    var arr = [{a:1,b:2,c:3},{a:4,b:5,c:6},{a:7,b:8,c:9}]
    for(var a = 0;a <arr.length;a++){
    	delete arr[a]["a"]				
    }
    console.log(arr)//[{b: 2, c: 3}, {b: 5, c: 6},{b: 8, c: 9}]
    
    
    5、在数组指定位置添加元素
    var array = ["one", "two", "four"];
    console.log(array); // ["one", "two", "four"]
    //第一个参数指定位置,第二个参数指定要删除的元素,如果为0,则追加
    array.splice(2, 0, "three");
    console.log(array); // ["one", "two", "three", "four"]
    
    6. 1)根据type类型将相同的存储在一个数组里,2)根据时间排序,3)时间只有一条的时候追加一条相同的数据,bucketId递增,rate为0.
    
        const arr = [
            {"bucketId":98,"type":"collect","time":1543449800000,"rate":0.014},
            {"bucketId":98,"type":"collect","time":1543449600000,"rate":0.014},
            {"bucketId":98,"type":"share","time":1543449600000,"rate":0.016},
            {"bucketId":98,"type":"share","time":1543441600000,"rate":0.016},
            {"bucketId":98,"type":"use","time":1543449700000,"rate":0.011},
            {"bucketId":98,"type":"use","time":1543449500000,"rate":0.011},
            {"bucketId":99,"type":"collect","time":1543449600000,"rate":0.014},
            {"bucketId":99,"type":"use","time":1543449600000,"rate":0.012},
            {"bucketId":99,"type":"share","time":1543449600000,"rate":0.012},
            {"bucketId":99,"type":"click","time":1543449600000,"rate":0.012},
        ]
    
            function demo (arr) {
                //分类
                const list = {};
                arr.forEach((item) => {
                    if ( !list[item.type] ) {
                        list[item.type] = [];
                        list[item.type].push(item);
                    } else {
                        list[item.type].push(item)
                    }
                });
                for(var key in list){
                    //排序
                    list[key].sort(function(n1,n2){
                        return n1.time-n2.time
                    })
                    //添加
                    if(list[key].length == 1){
                        list[key].push({
                            bucketId: list[key][0].bucketId + 1,
                            type:list[key][0].type,
                            time:list[key][0].time,
                            rate: 0,
                        })
                    }
                        
                } 
                console.log(list);
            };
            demo(arr);
    
    
    
    
    
    
    
  • 相关阅读:
    《大型网站技术架构》读后感
    质量属性
    课堂作业02
    课堂作业01
    《软件构架实践》读后感06
    《软件构架实践》读后感05
    Storm系列三: Storm消息可靠性保障
    Storm系列二: Storm拓扑设计
    Storm系列一: Storm初步
    网络层协议
  • 原文地址:https://www.cnblogs.com/yinxingen/p/9253051.html
Copyright © 2011-2022 走看看