Angular2 - summarize property values ​​in an object sent from observable

in my service.component.j . I am returning a Student observable array from an http call. The student object has a property called age,

student.ts

export class Student {
  id: number;
  name: string;
  age:number;
}

service.componnet.js

getStudents (): Observable< Student[]> 

In studentManagement.component.ts , which is an observer above the observed, I want to summarize the age of the students. I know that I can put sum () in the source code (which is less desirable, since I also need to display other information about Students on the page, for example, identifier, name, individual age) or calculate it from _studentList. Other than these two, in any other way?

private _studentList:Student[]=[];

    .subscribe(
            returnedStudents => {
              console.log(returnedStudents);
              this._studentList = returnedStudents;
            },
            erroMsg => this._errorMessage = erroMsg
          ) 
+4
2

map Observable .

getStudents().map(arr => arr.reduce((a, b) => a + b.age, 0));

. Observable<number>, .

:

getStudents (): Observable< Student[]> {
   return this.http.get(/*...*/).share();
}

getAgeSum(): Observable<number> {
    return this.studentsObservable
       .map(arr => arr.reduce((a, b) => a + b.age, 0));
}

, Observable HTTP-.

+6

map, , . - :

studentsObservable.map((students) => {
  return {
    ages: this.getAge(students),
    students: students
  };
});
+1

All Articles