distinct()

The distinct linq operator returns a new linq object that contains only the distinct elements of the collection--that is, only the first of each set of duplicate objects in the collection are included in the results. The distinct operator can also take a comparer function that controls how elements are considered duplicates.
var arr1 = [1, 1, 2, 2, 3, 3, 4, 4, 5, 5];

// col1 = [1, 2, 3, 4, 5]
var col1 = $linq(arr1).distinct().toArray();

var arr2 = ["one", "ONE", "two", "TWO", "three", "four"];

// col2 = ["one", "two", "three", "four"]
var col2 = $linq(arr2).distinct("(x, y) => x.toLowerCase() == y.toLowerCase()").toArray();

distinctBy()

The distinctBy linq operator returns a new linq object that contains only the elements of the collection that are distinct with respect to the key projected by the "key selector" function. The distinctBy operator can also take a comparer function that controls how the projected keys are considered duplicates.
var arr = [{number: 1, word: 'one'},
    {number: 1, word: 'ONE'},
    {number: 2, word: 'TWO'},
    {number: 2, word: 'two'},
    {number: 3, word: 'three'}];

// col1 = ['one', 'TWO', 'three']
var col1 = $linq(arr)
    .distinctBy("x => x.number")
    .select("x => x.word")
    .toArray();

// col2 = ['one', 'TWO', 'three']
var col2 = $linq(arr)
    .distinctBy("x => x.word", "(x, y) => x.toLowerCase() == y.toLowerCase()")
    .select("x => x.word")
    .toArray();

Last edited Dec 18, 2013 at 7:30 PM by battousai999, version 4