groupBy3(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. The keys are compared by using a specified comparer.



Type: function(value, key, arg)
A function to extract the key for each element.
A function to create a result value from each group.
Type: function(value, key, arg): boolean
An equality comparer to compare keys with.
Return Value

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


var petsList = [
    { name: "Barley", age: 8.3 },
    { name: "Boots", age: 4.9 },
    { name: "Whiskers", age: 1.5 },
    { name: "Daisy", age: 4.3 } ];

// Group Pet objects by the Math.Floor of their 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).groupBy3(
    function(pets, age) {
        var petsAges = $from(pets).select("$age");
        return {
            key: age,
            count: pets.count(),
            min: petsAges.min(),
            max: petsAges.max()

// Iterate over each anonymous type.
query.each(function(result) {
	document.writeln("Age 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:36 AM by suckgamoni, version 2


No comments yet.