This project is read-only.

distinct([comparer, arg])

Returns distinct elements from a sequence by using a specified equality comparer to compare values.

Syntax

Parameters

comparer
Type: function(a, b, arg): boolean
An equality comparer to compare values.
arg
An external argument.
Return Value

An iteratable object that contains distinct elements from the source sequence.

Examples

var ages = [ 21, 46, 46, 55, 17, 21, 55, 55 ];

var distinctAges = $from(ages).distinct();

document.writeln("Distinct ages:");

distinctAges.each("document.writeln($)");

/*
 This code produces the following output:

 Distinct ages:
 21
 46
 55
 17
*/

var products = [ { name: "apple", code: 9 }, 
                 { name: "orange", code: 4 }, 
                 { name: "apple", code: 9 }, 
                 { name: "lemon", code: 12 } ];

//Exclude duplicates.

var comparer = "#0 == #1 || (#0 && #1 && #0.name == #1.name && #0.code == #1.code)";

var noduplicates =
    $from(products).distinct(comparer);

noduplicates.each("document.writeln($name + ' ' + $code)");

/*
    This code produces the following output:
    apple 9 
    orange 4
    lemon 12
*/

Last edited May 16, 2012 at 6:07 AM by suckgamoni, version 1

Comments

No comments yet.