distinct([comparer, arg])

suckgamoni edited this page May 14, 2013 · 1 revision

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 iterable object that contains distinct elements from the source sequence.


Example

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

console.log("Distinct ages:");
from(ages).distinct().each("console.log($)");

/*
 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)";

from(products)
    .distinct(comparer)
    .each("console.log($name + ' ' + $code)");

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