How to find sum using reduce, pipe and combinelatest?

Delfin :

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));
fridoo :

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))
);

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=31951&siteId=1