2个数组的数组合并

1.情景:

//想要的数据
let arr1=[[01:1],[02:3],[03:12],[04:22]]

//接口只返回了2条数据
let arr2=[[02:33],[03:55]]

//于是,先分别做出2个对象,然后对象组合(注意有去重),后再把对象转成数据
function objArr(res) {  
        let series0 = {};
    res.forEach(t => {
        let dataTime = new Date(t.dataTime).getDate().toString().padStart(2, '0')
        series0[dataTime] = t.stopHours

    });
        

        //获取当前月的总天数,作为新建数组和对象的长度
    let d = new Date(res[0].dataTime)
    let year = d.getFullYear()
    let month = d.getMonth() + 1
    let days = new Date(year, month, 0).getDate()
    let seriesObj = {}

    // console.log(days);

    let categoryAll = Array.from({
        length: days
    }, (v, k) => {
        let key = (k + 1).toString().padStart(2, '0')
        seriesObj[key] = 0
        return (k + 1).toString().padStart(2, '0')

    });

    Object.assign(seriesObj, series0)
    let seriesArr = []
    for (const key in seriesObj) {
        if (seriesObj.hasOwnProperty.call(seriesObj, key)) {
            const t = seriesObj[key];
            seriesArr.push([key, t])

        }
    }
    // console.log(categoryAll,seriesArr);
}

--by Affandi ⊙▽⊙

你可能感兴趣的:(2个数组的数组合并)