This project is read-only.

groupBy2(valueSelector, keySelector[, comparer, arg])

Groups the elements of a sequence according to a key selector function. The keys are compared by using a comparer and each group's elements 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 an the result.
keySelector
Type: function(value, key, arg)
A function to extract the key for each element.
comparer
Type: function(key1, key2, arg): boolean
An equality comparer to compare keys.
arg
An external argument.
Return Value

An iteratable object contains keys and a collection of objects of values.

Examples


// Create a list of pets.
var pets = [
    { name: "Barley", age: 8 },
    { name: "Boots", age: 4 },
    { name: "Whiskers", age: 1 },
    { name: "Daisy", age: 4 } ];

// Group the pets using Age as the key value 
// and selecting only the pet's Name for each value.
var query = $from(pets).groupBy2("$name", "$age");

// Iterate over each IGrouping in the collection.
query.each(function(v, k) {
	// Print the key value of the IGrouping.
	document.writeln(k);
	// Iterate over each value in the 
	// IGrouping and print the value.
	v.each("document.writeln('  ' + $)");
});

/*
 This code produces the following output:

 8
   Barley
 4
   Boots
   Daisy
 1
   Whiskers
*/

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

Comments

No comments yet.