-3

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?

The KNVB
  • 3,588
  • 3
  • 29
  • 54
  • How to achieve it? By writing code. – Dexygen Jul 21 '23 at 07:31
  • In the code you pasted for your object, for most items `students` is an array but for the last one it is an object; is that correct? It would be better if data are consistent therefore and `students` is always an array, even if there is just one student. – secan Jul 21 '23 at 07:31

3 Answers3

0

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)
The KNVB
  • 3,588
  • 3
  • 29
  • 54
-2

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));
Hoang Long
  • 446
  • 4
  • 5
-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);
trincot
  • 317,000
  • 35
  • 244
  • 286