JSON数据根据字段分组并排序

[{
    "controlGroupId": 0,
    "controlGroupName": "基础数据",
    "controlSortId": 0,
    "controlType": "CtlComboBoxEdit",
    "domain": "CodeAirportAiracType"
},{
    "controlGroupId": 0,
    "controlGroupName": "基础数据",
    "controlSortId": 2,
    "controlType": "CtlComboBoxEdit",
    "domain": "CodeAirportAiracType"
}, {
    "controlGroupId": 2,
    "controlGroupName": "高级",
    "controlSortId": 6,
    "controlType": "CtlComboBoxEdit",
    "domain": "CodeAirportAiracType"
}, {
    "controlGroupId": 2,
    "controlGroupName": "高级",
    "controlSortId": 9,
    "controlType": "CtlComboBoxEdit",
    "domain": "CodeAirportAiracType"
}]

这是例子真实情况controlGroupId可能有多个不确定的值。
举例:controlGroupId为0的分为一组,为2的分为一组,取controlGroupName为组名,并且组之间升序。
各组之中再根据controlSortId升序。
这个例子期望的结果为

 [
    {
        title: "基础数据",
        group: [
            {
                controlGroupId: 0,
                controlGroupName: "基础数据",
                controlSortId: 0,
                controlType: "CtlComboBoxEdit",
                domain: "CodeAirportAiracType"
            },
            {
                controlGroupId: 0,
                controlGroupName: "基础数据",
                controlSortId: 2,
                controlType: "CtlComboBoxEdit",
                domain: "CodeAirportAiracType"
            }
        ]
    },
    {
        title: "高级",
        group: [
            {
                controlGroupId: 2,
                controlGroupName: "高级",
                controlSortId: 6,
                controlType: "CtlComboBoxEdit",
                domain: "CodeAirportAiracType"
            },
            {
                controlGroupId: 2,
                controlGroupName: "高级",
                controlSortId: 9,
                controlType: "CtlComboBoxEdit",
                domain: "CodeAirportAiracType"
            }
        ]
    }
];
阅读 2.8k
2 个回答

写的有点糙, 但是运行了没问题, 你把上面数据传进来就会 return 你想要的结构了

function test(data){
                let result1 = {},
                    result2 = [];
                
                data.map(item=>{
                    if(result1[item.controlGroupName]){
                        result1[item.controlGroupName].push(item)
                    }else{
                        result1[item.controlGroupName] =[];
                        result1[item.controlGroupName].push(item)
                    }
                })
                
                for(let item in result1){
                    let obj = {
                        title:item,
                        group:[]
                    }
                    result1[item].map(i=>{
                        obj.group.push(i)
                    })
                    result2.push(obj)
                }
                // 升序
                result2.sort((a,b)=>{
                 return a.group[0].controlGroupId - b.group[0].controlGroupId
                })
                
                return result2
            }
let before = [{
      'controlGroupId': 2,
      'controlGroupName': '高级',
      'controlSortId': 6,
      'controlType': 'CtlComboBoxEdit',
      'domain': 'CodeAirportAiracType'
    }, {
      'controlGroupId': 0,
      'controlGroupName': '基础数据',
      'controlSortId': 2,
      'controlType': 'CtlComboBoxEdit',
      'domain': 'CodeAirportAiracType'
    }, {
      'controlGroupId': 0,
      'controlGroupName': '基础数据',
      'controlSortId': 0,
      'controlType': 'CtlComboBoxEdit',
      'domain': 'CodeAirportAiracType'
    }, {
      'controlGroupId': 2,
      'controlGroupName': '高级',
      'controlSortId': 9,
      'controlType': 'CtlComboBoxEdit',
      'domain': 'CodeAirportAiracType'
    }, {
      'controlGroupId': 3,
      'controlGroupName': '根据这个命名',
      'controlSortId': 5,
      'controlType': 'CtlComboBoxEdit',
      'domain': 'CodeAirportAiracType'
    }, {
      'controlGroupId': 3,
      'controlGroupName': '根据这个命名',
      'controlSortId': 1,
      'controlType': 'CtlComboBoxEdit',
      'domain': 'CodeAirportAiracType'
    }]
    let after = transform(before)
    console.log(after)
transform (before) {
    // 用来存储结果
    let after = []
    // 用来记录已经加进after的ID
    let record = []
    // 遍历原数据
    before.forEach((item) => {
      let isNewGroup = true
      // 看看是不是已经记过
      record.forEach((id) => {
        if (id === item.controlGroupId) isNewGroup = false
      })
      // 如果没记过就加进after
      if (isNewGroup) {
        record.push(item.controlGroupId)
        let obj = { title: item.controlGroupName, group: [] }
        before.forEach((i) => {
          if (i.controlGroupId === item.controlGroupId) obj.group.push(i)
        })
        // 组内根据controlSortId升序
        obj.group.sort((a, b) => { return a.controlSortId - b.controlSortId })
        after.push(obj)
      }
    })
    // 结果根据controlGroupId升序
    after.sort((a, b) => { return a.group[0].controlGroupId - b.group[0].controlGroupId })
    
    return after
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题