Delfin
Delfin

Reputation: 607

How to find sum using reduce, pipe and combinelatest?

Values is list of obervables over input fields

    var example = combineLatest(values);

How do I find sum of values in text field.

    example.subscribe(val => {
        console.log('Sum:', val);
    });

Having subscriber over it gives me output of the form

Sum: (2) ["1", "2"]

Piping over combineLatest gives me NaN

     .pipe(reduce((acc, one) => {
         var a =Number(acc) + Number(one);
         console.log(a);
         return a;
     }, 0));

Upvotes: 0

Views: 824

Answers (1)

frido
frido

Reputation: 14099

You don't have to reduce the Observable, you have to reduce the array it emits.

var example = combineLatest(values).pipe(
  map(array => array.reduce((pv, cv) => pv + Number(cv), 0))
);

Upvotes: 3

Related Questions