0
# Observable Aggregation
1
2
Operators for aggregating observable sequences including reduce, count, min, max, and statistical operations.
3
4
## Capabilities
5
6
### Reduce/Aggregate
7
8
Reduces the observable sequence to a single value.
9
10
```javascript { .api }
11
/**
12
* Applies an accumulator function over the sequence and returns the final result
13
* @param {function} accumulator - Accumulator function taking current accumulation and next value
14
* @param {*} [seed] - Initial accumulator value
15
* @returns {Observable} Observable sequence containing the final accumulated result
16
*/
17
observable.reduce = function(accumulator, seed);
18
observable.aggregate = function(accumulator, seed); // Alias for reduce
19
```
20
21
**Usage Example:**
22
23
```javascript
24
var source = Rx.Observable.range(1, 5);
25
var sum = source.reduce(function(acc, x) { return acc + x; }, 0);
26
// Emits: 15 (1+2+3+4+5)
27
```
28
29
### Count
30
31
Counts the number of elements in the sequence.
32
33
```javascript { .api }
34
/**
35
* Returns the count of elements in the observable sequence
36
* @param {function} [predicate] - Function to test each element
37
* @param {*} [thisArg] - Object to use as this when executing predicate
38
* @returns {Observable} Observable sequence containing the count
39
*/
40
observable.count = function(predicate, thisArg);
41
```
42
43
### Sum
44
45
Computes the sum of numeric values.
46
47
```javascript { .api }
48
/**
49
* Computes the sum of a sequence of numeric values
50
* @param {function} [keySelector] - Function to transform elements before summing
51
* @param {*} [thisArg] - Object to use as this when executing keySelector
52
* @returns {Observable} Observable sequence containing the sum
53
*/
54
observable.sum = function(keySelector, thisArg);
55
```
56
57
### Average
58
59
Computes the average of numeric values.
60
61
```javascript { .api }
62
/**
63
* Computes the average of a sequence of numeric values
64
* @param {function} [keySelector] - Function to transform elements before averaging
65
* @param {*} [thisArg] - Object to use as this when executing keySelector
66
* @returns {Observable} Observable sequence containing the average
67
*/
68
observable.average = function(keySelector, thisArg);
69
```
70
71
### Min/Max
72
73
Finds the minimum or maximum value.
74
75
```javascript { .api }
76
/**
77
* Returns the minimum value in the sequence
78
* @param {function} [comparer] - Comparer function to determine order
79
* @returns {Observable} Observable sequence containing the minimum value
80
*/
81
observable.min = function(comparer);
82
83
/**
84
* Returns the maximum value in the sequence
85
* @param {function} [comparer] - Comparer function to determine order
86
* @returns {Observable} Observable sequence containing the maximum value
87
*/
88
observable.max = function(comparer);
89
```
90
91
### Min By/Max By
92
93
Finds elements with minimum or maximum key values.
94
95
```javascript { .api }
96
/**
97
* Returns elements with the minimum key value
98
* @param {function} keySelector - Function to compute comparison key
99
* @param {function} [comparer] - Comparer function for keys
100
* @returns {Observable} Observable sequence containing elements with minimum key
101
*/
102
observable.minBy = function(keySelector, comparer);
103
104
/**
105
* Returns elements with the maximum key value
106
* @param {function} keySelector - Function to compute comparison key
107
* @param {function} [comparer] - Comparer function for keys
108
* @returns {Observable} Observable sequence containing elements with maximum key
109
*/
110
observable.maxBy = function(keySelector, comparer);
111
```
112
113
### First/Last
114
115
Gets the first or last element.
116
117
```javascript { .api }
118
/**
119
* Returns the first element in the sequence
120
* @param {function} [predicate] - Function to test each element
121
* @param {*} [thisArg] - Object to use as this when executing predicate
122
* @returns {Observable} Observable sequence containing the first element
123
*/
124
observable.first = function(predicate, thisArg);
125
126
/**
127
* Returns the last element in the sequence
128
* @param {function} [predicate] - Function to test each element
129
* @param {*} [thisArg] - Object to use as this when executing predicate
130
* @returns {Observable} Observable sequence containing the last element
131
*/
132
observable.last = function(predicate, thisArg);
133
```
134
135
### First Or Default/Last Or Default
136
137
Gets the first or last element, or a default value.
138
139
```javascript { .api }
140
/**
141
* Returns the first element or a default value if none found
142
* @param {function} [predicate] - Function to test each element
143
* @param {*} [defaultValue] - Default value if no element found
144
* @param {*} [thisArg] - Object to use as this when executing predicate
145
* @returns {Observable} Observable sequence containing the first element or default
146
*/
147
observable.firstOrDefault = function(predicate, defaultValue, thisArg);
148
149
/**
150
* Returns the last element or a default value if none found
151
* @param {function} [predicate] - Function to test each element
152
* @param {*} [defaultValue] - Default value if no element found
153
* @param {*} [thisArg] - Object to use as this when executing predicate
154
* @returns {Observable} Observable sequence containing the last element or default
155
*/
156
observable.lastOrDefault = function(predicate, defaultValue, thisArg);
157
```
158
159
### Some/Every
160
161
Tests whether any or all elements match a predicate.
162
163
```javascript { .api }
164
/**
165
* Tests whether any element matches the specified predicate
166
* @param {function} [predicate] - Function to test each element
167
* @param {*} [thisArg] - Object to use as this when executing predicate
168
* @returns {Observable} Observable sequence containing boolean result
169
*/
170
observable.some = function(predicate, thisArg);
171
observable.any = function(predicate, thisArg); // Alias for some
172
173
/**
174
* Tests whether all elements match the specified predicate
175
* @param {function} [predicate] - Function to test each element
176
* @param {*} [thisArg] - Object to use as this when executing predicate
177
* @returns {Observable} Observable sequence containing boolean result
178
*/
179
observable.every = function(predicate, thisArg);
180
observable.all = function(predicate, thisArg); // Alias for every
181
```
182
183
### Contains/Includes
184
185
Tests whether the sequence contains a specific value.
186
187
```javascript { .api }
188
/**
189
* Tests whether the sequence contains the specified value
190
* @param {*} value - Value to search for
191
* @param {number} [fromIndex] - Index to start searching from
192
* @returns {Observable} Observable sequence containing boolean result
193
*/
194
observable.contains = function(value, fromIndex);
195
observable.includes = function(value, fromIndex); // Alias for contains
196
```
197
198
### Is Empty
199
200
Tests whether the sequence is empty.
201
202
```javascript { .api }
203
/**
204
* Tests whether the sequence is empty
205
* @returns {Observable} Observable sequence containing boolean result
206
*/
207
observable.isEmpty = function();
208
```
209
210
### To Array
211
212
Converts the sequence to an array.
213
214
```javascript { .api }
215
/**
216
* Creates an array from the observable sequence
217
* @returns {Observable} Observable sequence containing the array
218
*/
219
observable.toArray = function();
220
```
221
222
### Sequence Equal
223
224
Tests whether two sequences are equal.
225
226
```javascript { .api }
227
/**
228
* Tests whether two sequences are equal
229
* @param {Observable} other - Other sequence to compare with
230
* @param {function} [comparer] - Function to compare elements for equality
231
* @returns {Observable} Observable sequence containing boolean result
232
*/
233
observable.sequenceEqual = function(other, comparer);
234
```