最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Angular2 - sum the values of a property in the Object sent from an observerable - Stack Overflow

programmeradmin4浏览0评论

in my serviceponent.js I return an observable of an array of Student from the http call. Student object has a property called age,

student.ts

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

serviceponnet.js

getStudents (): Observable< Student[]> 

In the studentManagementponent.ts which is an observer to the observable above, I want to sum the age of the students. I know I can put a sum() in source (which is less preferred, as I also need to display other info of the Students in the page, such as id, name ,individual age as well.) or calculate it from _studentList. Besides these two, any other way?

private _studentList:Student[]=[];

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

in my service.ponent.js I return an observable of an array of Student from the http call. Student object has a property called age,

student.ts

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

service.ponnet.js

getStudents (): Observable< Student[]> 

In the studentManagement.ponent.ts which is an observer to the observable above, I want to sum the age of the students. I know I can put a sum() in source (which is less preferred, as I also need to display other info of the Students in the page, such as id, name ,individual age as well.) or calculate it from _studentList. Besides these two, any other way?

private _studentList:Student[]=[];

    .subscribe(
            returnedStudents => {
              console.log(returnedStudents);
              this._studentList = returnedStudents;
            },
            erroMsg => this._errorMessage = erroMsg
          ) 
Share Improve this question edited May 9, 2016 at 10:20 Luka Jacobowitz 23.6k5 gold badges40 silver badges58 bronze badges asked May 9, 2016 at 7:26 HammerHammer 8,89812 gold badges47 silver badges80 bronze badges 1
  • Could you please add more code. I don't see what you want to sum and what result you want to get besides the sum. – Günter Zöchbauer Commented May 9, 2016 at 7:28
Add a ment  | 

2 Answers 2

Reset to default 6

You could use map on the Observable to return the sum.

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

Here we reduce the inner Array to the sum of the age property of the students. Now what we have is a Observable<number>, which will emit the sum of the ages.

If you want to display both:

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

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

We have to call share here, so that the Observable bees hot and doesn't do the HTTP Request twice.

You could use a map function that returns both student list and age sums. Something like that:

studentsObservable.map((students) => {
  return {
    ages: this.getAge(students),
    students: students
  };
});
发布评论

评论列表(0)

  1. 暂无评论