js排序--一道js數據結構題

給一個數組:數組

[{ GroupMark: "", GroupName: "hao", SendTime: '123', SendContent: "1ha" },
{ GroupMark: "1", GroupName: "hao", SendTime: '123', SendContent: "2ha" },
{ GroupMark: "1", GroupName: "hao", SendTime: '123', SendContent: "3ha" },
{ GroupMark: "22", GroupName: "hao", SendTime: '123', SendContent: "4ha" },
{ GroupMark: "3", GroupName: "hao", SendTime: '123', SendContent: "6ha" },
{ GroupMark: "22", GroupName: "hao", SendTime: '123', SendContent: "5ha" }]

目標spa

[{"GroupMark":"","GroupName":"hao","SendTime":"123","SendContent":["1ha"]},
{"GroupMark":"1","GroupName":"hao","SendTime":"123","SendContent":["2ha","3ha"]},
{"GroupMark":"22","GroupName":"hao","SendTime":"123","SendContent":["4ha","5ha"]},
{"GroupMark":"3","GroupName":"hao","SendTime":"123","SendContent":["6ha"]}]

//參照數組去重的
var
arr =[{ GroupMark: "", GroupName: "hao", SendTime: '123', SendContent: "1ha" }, { GroupMark: "1", GroupName: "hao", SendTime: '123', SendContent: "2ha" }, { GroupMark: "1", GroupName: "hao", SendTime: '123', SendContent: "3ha" }, { GroupMark: "22", GroupName: "hao", SendTime: '123', SendContent: "4ha" }, { GroupMark: "3", GroupName: "hao", SendTime: '123', SendContent: "6ha" }, { GroupMark: "22", GroupName: "hao", SendTime: '123', SendContent: "5ha" }] var tempObj ={} var resArr = [] for(let [index,item] of arr.entries() ){ let {GroupMark,GroupName,SendContent} = item let keyStr = GroupMark+'&'+GroupName if(tempObj[keyStr]){ let tempSendContent= tempObj[keyStr].SendContent tempObj[keyStr].SendContent=[...tempSendContent,SendContent] }else{ tempObj[keyStr]={ ...item, SendContent:[SendContent] } } } for (let item in tempObj) { resArr.push(tempObj[item]) } console.log(resArr)
相關文章
相關標籤/搜索