码迷,mamicode.com
首页 > 编程语言 > 详细

js排序--一道js数据结构题

时间:2019-02-27 15:53:17      阅读:201      评论:0      收藏:0      [点我收藏+]

标签:play   time   color   code   item   control   去重   res   结构   

给一个数组:

[{ 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" }]

目标

[{"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)

 

js排序--一道js数据结构题

标签:play   time   color   code   item   control   去重   res   结构   

原文地址:https://www.cnblogs.com/forest-king/p/10444051.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!