js根据指定字段进行分组

js yekong

分组前数据

[
  {
    "buildingName": "北怡写字楼",
    "ageGrades": "18-24岁",
    "passengerFlow": 324
  },
  {
    "buildingName": "北怡写字楼",
    "ageGrades": "18岁以下",
    "passengerFlow": 124
  },
  {
    "buildingName": "北怡写字楼",
    "ageGrades": "25-44岁",
    "passengerFlow": 12
  },
  {
    "buildingName": "北怡写字楼",
    "ageGrades": "45-54岁",
    "passengerFlow": 42
  },
  {
    "buildingName": "北怡写字楼",
    "ageGrades": "55-64岁",
    "passengerFlow": 124
  },
  {
    "buildingName": "北怡写字楼",
    "ageGrades": "65岁以上",
    "passengerFlow": 124
  },
  {
    "buildingName": "光彩国际",
    "ageGrades": "18-24岁",
    "passengerFlow": 523
  },
  {
    "buildingName": "光彩国际",
    "ageGrades": "18岁以下",
    "passengerFlow": 124
  },
  {
    "buildingName": "光彩国际",
    "ageGrades": "25-44岁",
    "passengerFlow": 234
  },
  {
    "buildingName": "光彩国际",
    "ageGrades": "45-54岁",
    "passengerFlow": 352
  },
  {
    "buildingName": "光彩国际",
    "ageGrades": "55-64岁",
    "passengerFlow": 124
  },
  {
    "buildingName": "光彩国际",
    "ageGrades": "65岁以上",
    "passengerFlow": 52
  }
]

分组后

[
  {
    "buildingName": "北怡写字楼",
    "data": [
      {
        "buildingName": "北怡写字楼",
        "ageGrades": "18-24岁",
        "passengerFlow": 324
      },
      {
        "buildingName": "北怡写字楼",
        "ageGrades": "18岁以下",
        "passengerFlow": 124
      },
      {
        "buildingName": "北怡写字楼",
        "ageGrades": "25-44岁",
        "passengerFlow": 12
      },
      {
        "buildingName": "北怡写字楼",
        "ageGrades": "45-54岁",
        "passengerFlow": 42
      },
      {
        "buildingName": "北怡写字楼",
        "ageGrades": "55-64岁",
        "passengerFlow": 124
      },
      {
        "buildingName": "北怡写字楼",
        "ageGrades": "65岁以上",
        "passengerFlow": 124
      }
    ]
  },
  {
    "buildingName": "光彩国际",
    "data": [
      {
        "buildingName": "光彩国际",
        "ageGrades": "18-24岁",
        "passengerFlow": 523
      },
      {
        "buildingName": "光彩国际",
        "ageGrades": "18岁以下",
        "passengerFlow": 124
      },
      {
        "buildingName": "光彩国际",
        "ageGrades": "25-44岁",
        "passengerFlow": 234
      },
      {
        "buildingName": "光彩国际",
        "ageGrades": "45-54岁",
        "passengerFlow": 352
      },
      {
        "buildingName": "光彩国际",
        "ageGrades": "55-64岁",
        "passengerFlow": 124
      },
      {
        "buildingName": "光彩国际",
        "ageGrades": "65岁以上",
        "passengerFlow": 52
      }
    ]
  }
]

代码

getfenzu(arr) {
            let map = {}
            for (let i = 0; i < arr.length; i++) {
                let ai = arr[i]
                if (!map[ai.buildingName]) {
                    map[ai.buildingName] = [ai]
                } else {
                    map[ai.buildingName].push(ai)
                }
            }
            let res = []
            Object.keys(map).forEach(key => {
                res.push({
                    buildingName: key,
                    data: map[key],
                })
            })
            console.log(res)
            return res
        },
喜欢