根据接口返回的数据重组数组

 

let arr = [
    {
        createTime: "2020-10-16",
        imgPath:"http://1.jpg"
    },{
        createTime: "2020-10-16",
        imgPath:"2.jpg"
    },{
        createTime: "2020-10-17",
        imgPath:"3.jpg"
    },{
        createTime: "2020-10-17",
        imgPath:"4.jpg"
    },
]
function newFn(data){
    let arr = data;
    let obj = {}
    let result = [];
    for(let i = 0; i < arr.length; i++){
        if(!obj[arr[i].createTime]){
            result.push({createTime:arr[i].createTime,imgPath:[]})
            obj[arr[i].createTime] = arr[i].createTime
        }
    }
    return result;
}
function dealRes(data){
    for(let item of arr){
        for(let j of data){
            if(item.createTime == j.createTime){
                j.imgPath.push(item.imgPath)
            }
        }
    }
    return data;
}
let arrDeal = dealRes(newFn(arr));
console.log(arrDeal);

 

posted @ 2020-10-23 17:53  LaLaLa_heng  阅读(143)  评论(0编辑  收藏  举报