因为习惯太久的黑暗,所以阳光愈加刺眼!

空一座旧城,守一个旧人

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);






posted on 2018-07-02 11:34  空一座旧城,守一个旧人  阅读(181)  评论(0编辑  收藏  举报

导航