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

Posted five_trees

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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" }]

目标

[{"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数据结构题的主要内容,如果未能解决你的问题,请参考以下文章

js作用域的一道题的思考

一道经典JS题(关于this)

由一道小题谈谈JS的new操作符

一道常被人轻视的前端JS面试题

一道容易栽坑的有趣的面试题(关于js,定时器,闭包等)

一道常被人轻视的前端JS面试题