• js获取时间范围内不同粒度的所有时间并合并时间相同的数组


    var jsonData = [
                {
                    "timeDay": "2022-08-01 00:00:00",
                    "day": 1659283200000,
                    "cmcc": "31.88",
                    "cucc": "12.29",
                    "ctcc": 0,
                    "chcc": "10.38"
                },
                {
                    "timeDay": "2022-08-02 00:00:00",
                    "day": 1659369600000,
                    "cmcc": "31.88",
                    "cucc": "12.55",
                    "ctcc": "36.39",
                    "chcc": "2.29"
                },
                {
                    "timeDay": "2022-08-03 00:00:00",
                    "day": 1659456000000,
                    "cmcc": "31.88",
                    "cucc": "10.82",
                    "ctcc": "36.39",
                    "chcc": "8.40"
                },
                {
                    "timeDay": "2022-08-04 00:00:00",
                    "day": 1659542400000,
                    "cmcc": "31.88",
                    "cucc": "10.06",
                    "ctcc": "36.39",
                    "chcc": "8.68"
                },
                {
                    "timeDay": "2022-08-05 00:00:00",
                    "day": 1659628800000,
                    "cmcc": "31.88",
                    "cucc": "9.16",
                    "ctcc": "36.39",
                    "chcc": "15.37"
                }
            ]
    		console.log(jsonData,'jsonData')
            var padLeft = function( value, num, pad ) {
                    var result = value + '';
                    while( result.length < num ) {
                        result = (pad || '0') + result;
                    }
                    return result;
                }
            var formatDate= function( date, format ) {
                if( typeof(date) == 'number' || typeof(date) == 'string' ) {
                    date = new Date(date);
                }
                if (typeof(date) == 'undefined' || date == null) {return '-';}
                if( typeof(format) != 'string' ) format = null;
                return (format || 'YYYY-MM-DD HH:mm:ss').replace(/YYYY/g, padLeft(date.getFullYear(), 4))
                    .replace(/MM/g, padLeft(date.getMonth() + 1, 2))
                    .replace(/DD/g, padLeft(date.getDate(), 2))
                    .replace(/HH/g, padLeft(date.getHours(), 2))
                    .replace(/mm/g, padLeft(date.getMinutes(), 2))
                    .replace(/ss/g, padLeft(date.getSeconds(), 2));
            }
            var createDateX = function(starTime, endTime, scale){
                const xData = [];
                if(scale ==  24*60*60*1000) {
                	// 以天为粒度取当天零点
                    for (let nextTime = starTime; nextTime <= endTime; nextTime += scale) {
                        xData.push({timeDay:formatDate(new Date(new Date(nextTime).toLocaleDateString()).getTime()),day: new Date(new Date(nextTime).toLocaleDateString()).getTime()})
                    }
                } else {
                    for (let nextTime = starTime+scale; nextTime <= endTime+scale; nextTime += scale) {
                        xData.push({timeDay:formatDate(nextTime - nextTime%scale),day: nextTime - nextTime%scale})
                    }
                }
                
                // document.getElementById('id').innerHTML = xData
                return xData
            }
            let xData = createDateX(1657415922000 , 1660094322000 , 60*60*1000)
            console.log(xData,'console.log(xData) ') 
            var composeData = (target,source,otherItem) => {
                return target.map(item => {
                    const findItem = source.find(el => el.day == item.day);
                    return findItem || Object.assign(item,otherItem)
                }) 
            }
            
            //调用
            let newArr= composeData(xData,jsonData,{chcc: '0',cmcc: '0',ctcc: '0',cucc: '0'})
            console.log(newArr,'newArr')
        ····
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
  • 相关阅读:
    SQL Try Catch
    【入门篇】1.3 redis客户端之 jedis 高级使用示例
    HarmonyOS—HAP唯一性校验逻辑
    资源释放的方式(try - with - resource 和 try - catch - finally)
    2022锦江行——非繁城品:疫情之下,存量酒店的突围之道
    Spring Boot 2 :Spring Boot项目依赖(重点)&&连接Mysql(重点)
    AttributeError: module ‘OpenSSL.SSL’ has no attribute ‘SSLv3_METHOD
    【MATLAB源码-第44期】基于matlab的2*2MIMO-LDPC系统的误码率仿真。
    R语言求风险价值VaR Value at Risk
    IDEA通过原型(骨架)创建MavenJavaWeb项目
  • 原文地址:https://blog.csdn.net/weixin_43959024/article/details/126266604