zoukankan      html  css  js  c++  java
  • 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)

  • 相关阅读:
    Spring
    Spring
    Spring
    Spring
    JS 脱敏通用方法
    JS 实用技巧记录
    多快?好省!
    实战 | 如何使用微搭低代码实现按条件过滤数据
    2021腾讯数字生态大会落地武汉,微搭低代码专场等你来
    实战 | 如何使用微信云托管部署flask项目
  • 原文地址:https://www.cnblogs.com/forest-king/p/10444051.html
Copyright © 2011-2022 走看看