← All pipes
Numbers

weightedAverageBy

Weighted average with explicit nonnegative weights.

Use it in your component

Standalone component
import { Component } from '@angular/core';
import { WeightedAverageByPipe } from 'extra-pipe';

@Component({
  standalone: true,
  imports: [WeightedAverageByPipe],
  template: `{{ [{"value":2,"weight":1},{"value":4,"weight":3}] | weightedAverageBy: "value": "weight" }}`,
})
export class ExampleComponent {

}

Template
{{ [{"value":2,"weight":1},{"value":4,"weight":3}] | weightedAverageBy: "value": "weight" }}

Try it live.

The output below runs the packed library. Edit data, not code.

Use double quotes for text, or a number, array, object or null.
Ordered parameters: valueKey, weightKey. Locale is controlled separately.
This pipe has no locale override.
Live template data
readonly value = [
  {
    "value": 2,
    "weight": 1
  },
  {
    "value": 4,
    "weight": 3
  }
] as const;
readonly parameter0 = "value";
readonly parameter1 = "weight";
{{ value | weightedAverageBy: parameter0: parameter1 }}

Rendered output

3.5