This project is read-only.

groupBy4(valueSelector, keySelector, resultSelector[, comparer, arg])

Groups the elements of a sequence according to a specified key selector function and creates a result value from each group and its key. Key values are compared by using a specified comparer, and the elements of each group are projected by using a specified function.

Syntax

Parameters

valueSelector
Type: function(value, key, arg)
A function to map each source element to an element in the result.
keySelector
Type: function(value, key, arg)
A function to extract the key for each element.
resultSelector
Type: function(value, key, arg)
A function to create a result value from each group.
comparer
Type: function(key1, key2, arg): boolean
An equality comparer to compare keys with.
arg
An external argument
Return Value

A collection of elements of type TResult where each element represents a projection over a group and its key.

Examples

// Create a list of pets.
var petsList = [
    { name: "Barley", age: 8.3 },
    { name: "Boots", age: 4.9 },
    { name: "Whiskers", age: 1.5 },
    { name: "Daisy", age: 4.3 } ];

// Group Pet.Age values by the Math.Floor of the age.
// Then project an anonymous type from each group
// that consists of the key, the count of the group's
// elements, and the minimum and maximum age in the group.
var query = $from(petsList).groupBy4(
    "$age",
    "Math.floor($age)",
    function(ages, baseAge) {
        return {
            key: baseAge,
            count: ages.count(),
            min: ages.min(),
            max: ages.max()
        };
    }
);

// Iterate over each anonymous type.
query.each(function(result) {
    document.writeln("\nAge group: " + result.key);
    document.writeln("Number of pets in this age group: " + result.count);
    document.writeln("Minimum age: " + result.min);
    document.writeln("Maximum age: " + result.max);
});

/*  This code produces the following output:

	Age group: 8
	Number of pets in this age group: 1
	Minimum age: 8.3
	Maximum age: 8.3

	Age group: 4
	Number of pets in this age group: 2
	Minimum age: 4.3
	Maximum age: 4.9

	Age group: 1
	Number of pets in this age group: 1
	Minimum age: 1.5
	Maximum age: 1.5
*/

Last edited May 16, 2012 at 10:32 AM by suckgamoni, version 2

Comments

No comments yet.