Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

85
Views
Escanee una secuencia de objetos y agregue una propiedad con la suma acumulada en RxJS

Suponiendo que tengo un flujo fuente de objetos de la siguiente manera:

 source$ = from([{ id: 1, cost: 10},{ id: 2, cost: 5},{ id: 3, cost: 3},{ id: 4, cost: 10}])

En el observable derivado me gustaría mantener una propiedad adicional con una suma corriente del costo

 // Expected derived stream { id: 1, cost: 10, totalCost: 10} { id: 2, cost: 5, totalCost: 15} { id: 3, cost: 3, totalCost: 18} { id: 4, cost: 10, totalCost: 28}

¿Cómo podría lograrse con rxjs?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Me las arreglé para encontrar esta solución:

 import { from } from "rxjs"; import {scan} from "rxjs/internal/operators"; let source$ = from([{ id: 1, cost: 10},{ id: 2, cost: 5},{ id: 3, cost: 3},{ id: 4, cost: 10}]); source$ .pipe( scan((acc,curr) => { return {...curr, totalCost: acc.totalCost + curr.cost} }, {totalCost:0}) ) .subscribe(console.log)

https://codesandbox.io/s/rxjs-playground-bifurcado-3bdpd

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!