You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

JavaScript中按district_id整合学校与学生数据的实现方案

问题:如何在JavaScript中按学区整合学校数据?

原始数据

[{
    "school_id": "s1",
    "school_name": "school1",
    "district_id": "d1",
    "students": [{
        "student_id": "1",
        "student_name": "tony"
    }, {
        "student_id": "2",
        "student_name": "tom"
    }]
}, {
    "school_id": "s2",
    "school_name": "school2",
    "district_id": "d2",
    "students": [{
        "student_id": "1",
        "student_name": "march"
    }, {
        "student_id": "2",
        "student_name": "alex"
    }]
}, {
    "school_id": "s3",
    "school_name": "school3",
    "district_id": "d3",
    "students": [{
        "student_id": "1",
        "student_name": "bill"
    }, {
        "student_id": "2",
        "student_name": "bob"
    }]
}, {
    "school_id": "s4",
    "school_name": "school4",
    "district_id": "d3",
    "students": {
        "student_id": "1",
        "student_name": "tim"
    }
}]

需求说明

需要将同一district_id下的学校整合到一起,同时把每个学校的students字段重命名为studentList,最终得到如下结构:

[{
    "district_id": "d1",
    "schoolList": [{
        "school_id": "s1",
        "school_name": "school1",
        "studentList": [{
            "student_id": "1",
            "student_name": "tony"
        }, {
            "student_id": "2",
            "student_name": "tom"
        }]
    }]
}, {
    "district_id": "d2",
    "schoolList": [{
        "school_id": "s2",
        "school_name": "school2",
        "studentList": [{
            "student_id": "1",
            "student_name": "march"
        }, {
            "student_id": "2",
            "student_name": "alex"
        }]
    }]
}, {
    "district_id": "d3",
    "schoolList": [{
        "school_id": "s3",
        "school_name": "school3",
        "studentList": [{
            "student_id": "1",
            "student_name": "bill"
        }, {
            "student_id": "2",
            "student_name": "bob"
        }]
    }, {
        "school_id": "s4",
        "school_name": "school4",
        "studentList": {
            "student_id": "1",
            "student_name": "tim"
        }
    }]
}]

实现方案

可以通过reduce先按学区分组,再转换为目标结构,代码如下:

const schools = [/* 上述原始数据数组 */];

// 1. 按学区ID分组并转换学校结构
const groupedData = schools.reduce((acc, school) => {
    const districtId = school.district_id;
    // 初始化学区对应的学校数组
    if (!acc[districtId]) {
        acc[districtId] = [];
    }
    // 转换学校字段:重命名students为studentList,移除district_id
    const transformedSchool = {
        school_id: school.school_id,
        school_name: school.school_name,
        studentList: school.students
    };
    acc[districtId].push(transformedSchool);
    return acc;
}, {});

// 2. 将分组对象转为目标数组结构
const result = Object.entries(groupedData).map(([district_id, schoolList]) => ({
    district_id,
    schoolList
}));

console.log(result);

代码说明

  • 分组阶段:用reduce遍历原始数组,以district_id为键收集同学区的学校,同时完成字段重命名和冗余字段剔除。
  • 结构转换阶段:用Object.entries把分组后的键值对对象转为数组,再映射成需求要求的结构。

该方案兼容原始数据中students为数组或单个对象的情况,完全匹配目标输出。

内容的提问来源于stack exchange,提问作者sumitra_gangula

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.15 18:03:14