Skip to content

Latest commit

 

History

History
75 lines (62 loc) · 2.47 KB

groupby.md

File metadata and controls

75 lines (62 loc) · 2.47 KB

Rx.Observable.prototype.groupBy(keySelector, [elementSelector], [comparer])

Groups the elements of an observable sequence according to a specified key selector function and comparer and selects the resulting elements by using a specified function.

Arguments

  1. keySelector (Function): A function to extract the key for each element.
  2. [elementSelector] (Function): A function to map each source element to an element in an observable group.
  3. [comparer] (Any): Used to compare objects. If not specified, the default comparer is used.

Returns

(Observable): A sequence of observable groups, each of which corresponds to a unique key value, containing all elements that share that same key value.

Example

var codes = [
    { keyCode: 38}, // up
    { keyCode: 38}, // up
    { keyCode: 40}, // down
    { keyCode: 40}, // down
    { keyCode: 37}, // left
    { keyCode: 39}, // right
    { keyCode: 37}, // left
    { keyCode: 39}, // right
    { keyCode: 66}, // b
    { keyCode: 65}  // a
];

var source = Rx.Observable.from(codes)
    .groupBy(
        function (x) { return x.keyCode; },
        function (x) { return x.keyCode; });

var subscription = source.subscribe(
    function (obs) {
        // Print the count
        obs.count().subscribe(function (x) {
            console.log('Count: ' + x);
        });
    },
    function (err) {
        console.log('Error: ' + err);
    },
    function () {
        console.log('Completed');
    });

// => Count: 2
// => Count: 2
// => Count: 2
// => Count: 2
// => Count: 1
// => Count: 1
// => Completed

Location

File:

Dist:

NPM Packages:

NuGet Packages:

Unit Tests: