Estoy llamando al backend con esta llamada http:
this.StudentEnrollment.getRecordsById(list.value.split(/[\r\n]+/)).subscribe(values => { this.studentObject = values; });studenObject se parece a esto
{ records: [{name: james, school: USC, .....}, {name: Micheal, school: UCLA, ......},{name: John, school: UCLA, ......}], size: 3 }¿Es posible agrupar las escuelas y agregar un conteo y luego agregarlo a StudentObject? Entonces el objeto podría verse así:
[{school: UCLA, count: 2}, {school: USC, count: 1}]Usando el operador groupBy, podría hacer algo como esto:
data$ = this.StudentEnrollment.getRecordsById(list.value.split(/[\r\n]+/)); schoolsGrouped$ = data$.pipe( // Use concatMap to emit the array elements one by one for the groupBy concatMap(students => students), // Group by the student's school groupBy(student => student.school), // Merge each grouped set mergeMap(group => zip(of(group.key), group.pipe(toArray())).pipe( map(([key, students]) => ({ key, students })) ) ), // Emit one array of the grouped results toArray() );A continuación, podría acceder a los datos de esta manera:
constructor() { this.schoolsGrouped$.subscribe(schools => schools.forEach(school => console.log(school.key, school.students.length))) }Hay un ejemplo de trabajo aquí: https://stackblitz.com/edit/angular-schools-groupby-deborahk
Para agregar la información agrupada a la estructura de datos existente, puede hacer algo como esto (vea el operador de tap agregado:
schoolsGrouped$ = of(this.data.records).pipe( // Emit the array elements one by one for the groupBy concatMap(students => students), groupBy(student => student.school), // Merge each grouped set mergeMap(group => zip(of(group.key), group.pipe(toArray())).pipe( map(([key, students]) => ({ key, students })) ) ), // Add the result to the existing data object tap( school => (this.data = { ...this.data, schools: [ ...this.data.schools, { school: school.key, count: school.students.length }, ], }) ), // Emit one array of the grouped results toArray() );