improve average operator
This commit is contained in:
parent
2badf46e4b
commit
4a553e3cbb
|
@ -21,12 +21,23 @@ of({a: 1, b:2, e: 1}, {a: 0, c: 1, b:3}, {a: 0, d: 1, b:1})
|
|||
.subscribe((v) => console.log(v));
|
||||
*/
|
||||
|
||||
function $average() {
|
||||
function $average(cb) {
|
||||
return pipe(
|
||||
reduce((accum, curr) => ({
|
||||
sum: accum.sum + curr,
|
||||
reduce((accum, curr) => {
|
||||
let currentValue;
|
||||
if (typeof cb === 'string' || cb instanceof String){
|
||||
currentValue = curr[cb];
|
||||
} else if(typeof cb === "function") {
|
||||
currentValue = cb(curr);
|
||||
} else {
|
||||
currentValue = curr;
|
||||
}
|
||||
|
||||
return {
|
||||
sum: accum.sum + currentValue,
|
||||
count: accum.count + 1
|
||||
}), { sum: 0, count: 0 }),
|
||||
}
|
||||
}, { sum: 0, count: 0 }),
|
||||
map(o => o.sum / o.count)
|
||||
);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue