skip to Main Content

I have the following array of objects

[{
    "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"
    }
}]

I need to generate new repsonse by combining students belonging to the same school in the sae district as shown below

[{
    "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": "1",
            "student_name": "bill"
        }]
    }, {
        "school_id": "s4",
        "school_name": "school4",
        "studentList": {
            "student_id": "1",
            "student_name": "tim"
        }
    }]
}]

How do I achieve it?

3

Answers


  1. To achieve the desired result of combining students belonging to the same school in the same district, you can use JavaScript to iterate through the original array of objects and create a new response array based on the district and school information. Here’s a step-by-step solution:

        // Original array of objects
        const data = [
          {"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"}}
        ];
        
        // Create a new response array by combining students belonging to the same school in the same district
        const response = [];
        const districtMap = {};
        
        data.forEach((item) => {
          const { district_id, school_id, school_name, students } = item;
          
          // Check if the district already exists in the response array
          if (!districtMap[district_id]) {
            // If the district doesn't exist, add it to the response array
            districtMap[district_id] = {
              district_id,
              schoolList: []
            };
            response.push(districtMap[district_id]);
          }
          
          // Check if the school already exists in the district's schoolList
          const school = districtMap[district_id].schoolList.find((school) => school.school_id === school_id);
          if (!school) {
            // If the school doesn't exist, add it to the schoolList
            districtMap[district_id].schoolList.push({
              school_id,
              school_name,
              studentList: Array.isArray(students) ? students : [students]
            });
          } else {
            // If the school already exists, add students to its studentList
            school.studentList = school.studentList.concat(Array.isArray(students) ? students : [students]);
          }
        });
        
        console.log(JSON.stringify(response, null, 2));
    Login or Signup to reply.
  2. You can collect the unique districts ids as keys of a plain object, and associate the result object to each. Upon the first encounter of a district id, create the object for it (with empty schoolList array). Add each school to the schoolList array that corresponds to the district:

    const data = [{"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"}}]
    
    const districts = {};
    for (const {district_id, ...school} of data) {
        (districts[district_id] ??= { district_id, schoolList: [] })
            .schoolList.push(school);
    }
    const result = Object.values(districts);
    
    console.log(result);
    Login or Signup to reply.
  3. It is my solution.

    let data = [{
      "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"
      }
    }];
    let temp = {};
    let result = [];
    data.forEach(d => {
      if (temp[d.district_id] === undefined) {
        temp[d.district_id] = [];
      }
      temp[d.district_id].push({
        "school_name": d.school_name,
        school_id: d.school_id,
        students: d.students
      });
    });
    for (const [key, value] of Object.entries(temp)) {
      result.push({
        "district_id": key,
        school_list: value
      });
    }
    console.log(result)
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search