js排序--一道js数据结构题
2024-08-29 05:25:47
给一个数组:
[{ 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)
最新文章
- Bootstrap之样式风格与下拉菜单
- sql查询工程结算分包款转出
- (PHP)程序中如何判断当前用户终端是手机等移动终端
- MSSQL PIVOT 实现行列转置
- jQuery Validate 表单验证
- Linux和Windows路由配置
- solaris bind 符号未定义
- GitHub初次使用记录(一)
- Apache Mesos_百度百科
- 在不同Activity之间传递数据的四种常用方法
- 深入浅出KnockoutJS
- FMDB的一些基本操作小结
- 通过Servlet实现汉字验证码
- 将你的Python Web程序部署到Ubuntu服务器上
- 【一天一道LeetCode】#63. Unique Paths II
- python进阶之time模块详解
- 20175317 《Java程序设计》第六周学习总结
- 物流一站式查询之TrackingMore篇
- ceph:如何处理rados --striper上传失败的对象
- UVALive 6913 I Want That Cake 博弈dp