Options
All
  • Public
  • Public/Protected
  • All
Menu

Class HashSet<T>

Represents a set of values.

Type parameters

  • T

Hierarchy

Index

Constructors

constructor

  • Initializes a new instance of the HashSet<T> class that is empty and uses the default equality comparer for the set type.

    let hashset = new HashSet()
    

    Type parameters

    • T

    Returns HashSet<T>

  • Initializes a new instance of the HashSet<T> class that uses the specified equality comparer for the set type, contains elements copied from the specified collection.

    Type parameters

    • T

    Parameters

    • collection: IEnumerable<T>

      The collection whose elements are copied to the new set.

    • Optional comparer: IEqualityComparer<T>

      The IEqualityComparer<T> implementation to use when comparing values in the set, or null to use the default EqualityComparer<T> implementation for the set type.

      let nameComparer = {
      Equals: (a,b) => a.Name===b.Name,
      GetHashCode: a => a.Name
      }
      let hashset = new HashSet([{Name:"Jack", Age:20}, {Name:"John", Age:20}], nameComparer)
      

    Returns HashSet<T>

  • Initializes a new instance of the HashSet<T> class that uses the specified equality comparer for the set type.

    Type parameters

    • T

    Parameters

    • comparer: IEqualityComparer<T>

      The IEqualityComparer<T> implementation to use when comparing values in the set, or null to use the default IEqualityComparer<T> implementation for the set type.

      let nameComparer = {
      Equals: (a,b) => a.Name===b.Name,
      GetHashCode: a => a.Name
      }
      let hashset = new HashSet(nameComparer)
      

    Returns HashSet<T>

Properties

Readonly Comparer

Comparer: IEqualityComparer<T>

Gets the IEqualityComparer<T> object that is used to determine equality for the values in the set.

Readonly CountNative

CountNative: number

Gets the number of elements that are contained in a set.

let hs = new HashSet()
hs.Add(1)
// true
hs.Add(2)
// true
hs.CountNative
// 2

Methods

Add

  • Add(item: T): boolean
  • Adds the specified element to a set.

    Parameters

    • item: T

      The element to add to the set.

    Returns boolean

    true if the element is added to the HashSet<T> object; false if the element is already present.

    let hs = new HashSet()
    hs.Add(1)
    // true
    hs.Add(1)
    // false
    

Aggregate

  • Aggregate<TAccumulate, TResult>(seed: TAccumulate, func: (seed: TAccumulate, item: T) => TAccumulate, resultSelector?: (accumulate: TAccumulate) => TResult): TResult
  • Aggregate(func: (seed: T, item: T) => T): T
  • Applies an accumulator function over a sequence. The specified seed value is used as the initial accumulator value, and the specified function is used to select the result value.

    Type parameters

    • TAccumulate

    • TResult

    Parameters

    • seed: TAccumulate

      The initial accumulator value.

    • func: (seed: TAccumulate, item: T) => TAccumulate

      An accumulator function to be invoked on each element.

        • (seed: TAccumulate, item: T): TAccumulate
        • Parameters

          • seed: TAccumulate
          • item: T

          Returns TAccumulate

    • Optional resultSelector: (accumulate: TAccumulate) => TResult

      A function to transform the final accumulator value into the result value.

        • (accumulate: TAccumulate): TResult
        • Parameters

          • accumulate: TAccumulate

          Returns TResult

    Returns TResult

    The transformed final accumulator value.

    ["Jane", "Doe", "Joe"].Aggregate("", (res, item)=>res+" "+item, t=>t.toUpperCase().trim())
    // "JANE DOE JOE"
    
  • Applies an accumulator function over a sequence.

    Parameters

    • func: (seed: T, item: T) => T

      An accumulator function to be invoked on each element.

        • (seed: T, item: T): T
        • Parameters

          • seed: T
          • item: T

          Returns T

    Returns T

    The final accumulator value.

    ["Jane", "Doe", "Joe"].Aggregate((res, item)=>res+" "+item)
    // " Jane Doe Joe"
    

All

  • All(predicate: (item: T) => boolean): boolean
  • Determines whether all elements of a sequence satisfy a condition.

    Parameters

    • predicate: (item: T) => boolean

      A function to test each element for a condition.

        • (item: T): boolean
        • Parameters

          • item: T

          Returns boolean

    Returns boolean

    true if every element of the source sequence passes the test in the specified predicate, or if the sequence is empty; otherwise, false.

    [1,9,15].All(t=>t>5)
    // false
    [1,9,15].All(t=>t<50)
    // true
    

Any

  • Any(predicate?: (item: T) => boolean): boolean
  • Determines whether any element of a sequence exists or satisfies a condition.

    Parameters

    • Optional predicate: (item: T) => boolean

      A function to test each element for a condition.

        • (item: T): boolean
        • Parameters

          • item: T

          Returns boolean

    Returns boolean

    true if any elements in the source sequence pass the test in the specified predicate; otherwise, false.

    [1,9,15].Any(t=>t>5)
    // true
    [1,9,15].Any(t=>t>50)
    // false
    

Append

  • Appends a value to the end of the sequence.

    Parameters

    • element: T

      The value to append to source.

    Returns IEnumerable<T>

    A new sequence that ends with element.

    [1,9,15].Append(20).ToArray()
    // [1, 9, 15, 20]
    

AsEnumerable

  • Returns the input typed as IEnumerable<T>.

    Returns IEnumerable<T>

    The input sequence typed as IEnumerable<T>.

Average

  • Average(selector?: (item: T) => number): number
  • Computes the average of a sequence of values.

    Parameters

    • Optional selector: (item: T) => number

      A transform function to apply to each element.

        • (item: T): number
        • Parameters

          • item: T

          Returns number

    Returns number

    The average of the sequence of values, or NaN if the source sequence is empty or contains only values that are null.

    [1,9,15].Average()
    // 8.333333333333334
    

Cast

  • Casts the elements of an IEnumerable to the specified type. Useful for Typescript developers.

    Type parameters

    • TResult

    Returns IEnumerable<TResult>

    An IEnumerable<T> that contains each element of the source sequence cast to the specified type.

Clear

  • Clear(): void
  • Removes all elements from a HashSet<T> object.

    let hs = new HashSet()
    hs.Add(1)
    // true
    hs.Add(2)
    // true
    hs.Clear()
    hs.CountNative
    // 0
    

    Returns void

Concat

  • Concatenates two sequences.

    Parameters

    • second: IEnumerable<T>

      The sequence to concatenate to the first sequence.

    Returns IEnumerable<T>

    An IEnumerable<T> that contains the concatenated elements of the two sequences.

    [1,9,15].Concat([20,21,25]).ToArray()
    // [1, 9, 15, 20, 21, 25]
    

Contains

  • Determines whether a sequence contains a specified element.

    Parameters

    • value: T

      The value to locate in the sequence.

    • Optional comparer: IEqualityComparer<T>

      An IEqualityComparer<T> to compare values.

    Returns boolean

    true if the source sequence contains an element that has the specified value; otherwise, false.

    [1,9,15].Contains(9)
    // true
    

ContainsNative

  • ContainsNative(item: T): boolean
  • Determines whether a HashSet<T> object contains the specified element.

    Parameters

    • item: T

      The element to locate in the HashSet<T> object.

    Returns boolean

    true if the HashSet<T> object contains the specified element; otherwise, false.

    let hs = new HashSet()
    hs.Add(1)
    // true
    hs.ContainsNative(1)
    // true
    hs.ContainsNative(2)
    // false
    

CopyTo

  • CopyTo(array: T[], arrayIndex?: number, count?: number): void
  • Copies the specified number of elements of a HashSet<T> object to an array, starting at the specified array index.

    Parameters

    • array: T[]

      The one-dimensional array that is the destination of the elements copied from the HashSet<T> object. The array must have zero-based indexing.

    • Optional arrayIndex: number

      The zero-based index in array at which copying begins.

    • Optional count: number

      The number of elements to copy to array.

      let hs1 = new HashSet()
      hs1.Add(1)
      hs1.Add(2)
      hs1.Add(3)
      
      var setArr = [1,2,3,4,5,6,7,8,9]
      
      hs1.CopyTo(setArr, 4, 2)
      setArr
      // [1, 2, 3, 4, 1, 2, 7, 8, 9]
      

    Returns void

Count

  • Count(predicate?: (item: T) => boolean): number
  • Returns the number of elements in a sequence.

    Parameters

    • Optional predicate: (item: T) => boolean

      A function to test each element for a condition.

        • (item: T): boolean
        • Parameters

          • item: T

          Returns boolean

    Returns number

    The number of elements in the input sequence. A number that represents how many elements in the sequence satisfy the condition in the predicate function.

    [1,9,15].Count()
    // 3
    [1,9,15].Count(t=>t>5)
    // 2
    

CreateSetComparer

  • Returns an IEqualityComparer<HashSet<T>> object that can be used for equality testing of a HashSet<T> object.

    Returns IEqualityComparer<HashSet<T>>

    An IEqualityComparer<HashSet<T>> object that can be used for deep equality testing of the HashSet<T> object.

    let hs1 = new HashSet([new HashSet([1,2,3]), new HashSet([7,8,9]), new HashSet([4,5,6])])
    let hs2 = new HashSet([new HashSet([1,2,3]), new HashSet([7,8,9]), new HashSet([4,5,6])])
    let hs3 = new HashSet([new HashSet([1,2,3,4]), new HashSet([4,5,6]), new HashSet([7,8,9])])
    hs1.SequenceEqual(hs2, hs1.CreateSetComparer())
    // true
    hs1.SequenceEqual(hs3, hs1.CreateSetComparer())
    // false
    

Custom

  • Wrapper for custom projection or filtering.

    Type parameters

    • K

    Parameters

    • fun: (source: IEnumerable<T>) => () => IterableIterator<K>

      A generator function which yields transformed source.

        • Parameters

          Returns () => IterableIterator<K>

            • (): IterableIterator<K>
            • Returns IterableIterator<K>

    Returns IEnumerable<K>

    New IEnumerable<K> yielded from generator function.

    let people = [{Name:"Jack", Age:3}, {Name:"Joe", Age:2}, {Name:"Jane", Age:1}]
    people.Custom(function* () {
    // Output the names as many times as they are old
    for (let t of this)
    yield* Enumerable.Repeat(t.Name, t.Age)
    }).Select(t=>t.toUpperCase()).ToArray()
    // ["JACK", "JACK", "JACK", "JOE", "JOE", "JANE"]
    
  • Wrapper for custom aggregation of source sequence.

    Type parameters

    • K

    Parameters

    Returns K

    Final value aggregated in function.

    let people = [{Name:"Jack", Age:18}, {Name:"Joe", Age:22}, {Name:"Jane", Age:20}]
    let oldest = people.Custom((source)=>{
    // get the oldest person (aka MaxBy)
    let currOldest = source.FirstOrDefault();
    for (let t of source) if (t.Age > currOldest.Age) currOldest = t;
    return currOldest;
    })
    oldest
    // {Name:"Joe", Age:22}
    

DefaultIfEmpty

  • Returns the elements of an IEnumerable, or a default valued singleton collection if the sequence is empty.

    Parameters

    • Optional defaultValue: T

      The value to return if the sequence is empty.

    Returns IEnumerable<T>

    An IEnumerable<T> that contains defaultValue if source is empty; otherwise, source.

    [1,9,15].DefaultIfEmpty(5).ToArray()
    // [1,9,15]
    [].DefaultIfEmpty(5).ToArray()
    // [5]
    

Distinct

  • Returns distinct elements from a sequence.

    Parameters

    Returns IEnumerable<T>

    An IEnumerable<T> that contains distinct elements from the source sequence.

    [1,1,2,2,2,3,4,5,6,6].Distinct().ToArray()
    // [1, 2, 3, 4, 5, 6]
    

ElementAt

  • ElementAt(index: number): T
  • Returns the element at a specified index in a sequence.

    Parameters

    • index: number

      The zero-based index of the element to retrieve.

    Returns T

    The element at the specified position in the source sequence.

    [1,1,2,2,2,3,4,5,6,6].ElementAt(5)
    // 3
    

ElementAtOrDefault

  • ElementAtOrDefault(index: number, defaultValue?: T): T
  • Returns the element at a specified index in a sequence or a default value if the index is out of range.

    Parameters

    • index: number

      The zero-based index of the element to retrieve.

    • Optional defaultValue: T

      Default value if the index is out of range.

    Returns T

    Default value if the index is outside the bounds of the source sequence; otherwise, the element at the specified position in the source sequence.

    [1,1,2,2,2,3,4,5,6,6].ElementAtOrDefault(20, 123)
    // 123
    [1,1,2,2,2,3,4,5,6,6].ElementAtOrDefault(8, 123)
    // 6
    

Except

  • Produces the set difference of two sequences.

    Parameters

    • second: IEnumerable<T>

      An IEnumerable<T> whose elements that also occur in the first sequence will cause those elements to be removed from the returned sequence.

    • Optional comparer: IEqualityComparer<T>

      An IEqualityComparer<T> to compare values.

    Returns IEnumerable<T>

    A sequence that contains the set difference of the elements of two sequences.

    [1,1,2,2,2,3,4,5,6,6].Except([1,2,8]).ToArray()
    // [3, 4, 5, 6]
    

ExceptWith

  • Removes all elements in the specified collection from the current HashSet<T> object.

    Parameters

    • other: IEnumerable<T>

      The collection of items to remove from the HashSet<T> object.

      let hs1 = new HashSet([1,2,3,4,5])
      let hs2 = new HashSet([2,3,4])
      hs1.ExceptWith(hs2)
      hs1.ToArray()
      // [1, 5]
      

    Returns void

First

  • First(predicate?: (item: T) => boolean): T
  • Returns the first element of a sequence.

    Parameters

    • Optional predicate: (item: T) => boolean

      A function to test each element for a condition.

        • (item: T): boolean
        • Parameters

          • item: T

          Returns boolean

    Returns T

    The first element in the sequence that passes the test in the specified predicate function.

    [1,1,2,2,2,3,4,5,6,6].First()
    // 1
    [1,1,2,2,2,3,4,5,6,6].First(t=>t>2)
    // 3
    [].First()
    // Error - Uncaught No first element
    

FirstOrDefault

  • FirstOrDefault(predicate?: (item: T) => boolean, defaultValue?: T): T
  • Returns the first element of a sequence, or a default value if no element is found.

    Parameters

    • Optional predicate: (item: T) => boolean

      A function to test each element for a condition.

        • (item: T): boolean
        • Parameters

          • item: T

          Returns boolean

    • Optional defaultValue: T

      Default value if no element is found.

    Returns T

    Default value if source is empty or if no element passes the test specified by predicate; otherwise, the first element in source that passes the test specified by predicate.

    [1,1,2,2,2,3,4,5,6,6].FirstOrDefault(t=>true, 123)
    // 1
    [1,1,2,2,2,3,4,5,6,6].FirstOrDefault(t=>t>2, 123)
    // 3
    [].FirstOrDefault(t=>true, 123)
    // 123
    

ForEach

  • ForEach(callback: (item: T, index?: number) => void): void
  • Executes callback function on every element of a sequence.

    Parameters

    • callback: (item: T, index?: number) => void

      Function to be executed on every element of a sequence.

      [1,3,5].ForEach(t=>console.log(t))
      // 1
      // 3
      // 5
      
        • (item: T, index?: number): void
        • Parameters

          • item: T
          • Optional index: number

          Returns void

    Returns void

GroupBy

  • GroupBy<TKey, TElement, TResult>(keySelector: (item: T) => TKey, elementSelector: (item: T) => TElement, resultSelector: (key: TKey, group: IEnumerable<TElement>) => TResult, comparer?: IEqualityComparer<TKey>): IEnumerable<TResult>
  • GroupBy<TKey, TElement>(keySelector: (item: T) => TKey, elementSelector: (item: T) => TElement): IEnumerable<IGrouping<TKey, TElement>>
  • GroupBy<TKey, TElement>(keySelector: (item: T) => TKey, elementSelector: (item: T) => TElement, comparer: IEqualityComparer<TKey>): IEnumerable<IGrouping<TKey, TElement>>
  • GroupBy<TKey, TResult>(keySelector: (item: T) => TKey, resultSelector: (key: TKey, group: IEnumerable<T>) => TResult, comparer?: IEqualityComparer<TKey>): IEnumerable<TResult>
  • GroupBy<TKey>(keySelector: (item: T) => TKey, comparer?: IEqualityComparer<TKey>): IEnumerable<IGrouping<TKey, T>>
  • Groups the elements of a sequence according to a specified key selector function and creates a result value from each group and its key. Key values are compared by using a specified comparer, and the elements of each group are projected by using a specified function.

    Type parameters

    • TKey

    • TElement

    • TResult

    Parameters

    • keySelector: (item: T) => TKey

      A function to extract the key for each element.

        • (item: T): TKey
        • Parameters

          • item: T

          Returns TKey

    • elementSelector: (item: T) => TElement

      A function to map each source element to an element in an IGrouping<TKey,TElement>.

        • (item: T): TElement
        • Parameters

          • item: T

          Returns TElement

    • resultSelector: (key: TKey, group: IEnumerable<TElement>) => TResult

      A function to create a result value from each group.

        • Parameters

          Returns TResult

    • Optional comparer: IEqualityComparer<TKey>

      An IEqualityComparer<TKey> to compare keys with.

    Returns IEnumerable<TResult>

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

    let ageGroupEqualityComparer = {
    Equals: (x, y) => x.AgeGroup===y.AgeGroup,
    GetHashCode: obj => obj.AgeGroup
    }
    let people = [{Name:"Jack", Age:18}, {Name:"Joe", Age:22}, {Name:"Jack", Age:20}]
    people.GroupBy(t=>t.Age>20 ? {AgeGroup:'Young'} : {AgeGroup:'Old'}, t=>t.Name, (key, group)=>({Key:key, Names:[...group]}), ageGroupEqualityComparer).ToArray()
    // [{"Key":{"AgeGroup":"Old"},"Names":["Jack","Jack"]},{"Key":{"AgeGroup":"Young"},"Names":["Joe"]}]
    
  • Groups the elements of a sequence according to a specified key selector function and projects the elements for each group by using a specified function.

    Type parameters

    • TKey

    • TElement

    Parameters

    • keySelector: (item: T) => TKey

      A function to extract the key for each element.

        • (item: T): TKey
        • Parameters

          • item: T

          Returns TKey

    • elementSelector: (item: T) => TElement

      A function to map each source element to an element in the IGrouping<TKey,TElement>.

        • (item: T): TElement
        • Parameters

          • item: T

          Returns TElement

    Returns IEnumerable<IGrouping<TKey, TElement>>

    An IEnumerable<IGrouping<TKey, TElement>> where each IGrouping<TKey,TElement> object contains a collection of objects of type TElement and a key.

    let people = [{Name:"Jack", Age:18}, {Name:"Joe", Age:22}, {Name:"Jack", Age:20}]
    people.GroupBy(t=>t.Age>20 ? 'Young' : 'Old', t=>t.Name).Select(t=>({Key:t.Key, Names:t.ToArray()})).ToArray()
    // [{"Key":"Old","Names":["Jack","Jack"]},{"Key":"Young","Names":["Joe"]}]
    
  • 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.

    Type parameters

    • TKey

    • TElement

    Parameters

    • keySelector: (item: T) => TKey

      A function to extract the key for each element.

        • (item: T): TKey
        • Parameters

          • item: T

          Returns TKey

    • elementSelector: (item: T) => TElement

      A function to map each source element to an element in an IGrouping<TKey,TElement>.

        • (item: T): TElement
        • Parameters

          • item: T

          Returns TElement

    • comparer: IEqualityComparer<TKey>

      An IEqualityComparer<T> to compare keys.

    Returns IEnumerable<IGrouping<TKey, TElement>>

    An IEnumerable<IGrouping<TKey, TElement>> where each IGrouping<TKey,TElement> object contains a collection of objects of type TElement and a key.

    let ageGroupEqualityComparer = {
    Equals: (x, y) => x.AgeGroup===y.AgeGroup,
    GetHashCode: obj => obj.AgeGroup
    }
    let people = [{Name:"Jack", Age:18}, {Name:"Joe", Age:22}, {Name:"Jack", Age:20}]
    people.GroupBy(t=>t.Age>20 ? {AgeGroup:'Young'} : {AgeGroup:'Old'}, t=>t.Name, ageGroupEqualityComparer).Select(t=>({Key:t.Key, Names:t.ToArray()})).ToArray()
    [{"Key":{"AgeGroup":"Old"},"Names":["Jack","Jack"]},{"Key":{"AgeGroup":"Young"},"Names":["Joe"]}]
    //
    
  • 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 parameters

    • TKey

    • TResult

    Parameters

    • keySelector: (item: T) => TKey

      A function to extract the key for each element.

        • (item: T): TKey
        • Parameters

          • item: T

          Returns TKey

    • resultSelector: (key: TKey, group: IEnumerable<T>) => TResult

      A function to create a result value from each group.

    • Optional comparer: IEqualityComparer<TKey>

      An IEqualityComparer<T> to compare keys with.

    Returns IEnumerable<TResult>

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

    let ageGroupEqualityComparer = {
    Equals: (x, y) => x.AgeGroup===y.AgeGroup,
    GetHashCode: obj => obj.AgeGroup
    }
    let people = [{Name:"Jack", Age:18}, {Name:"Joe", Age:22}, {Name:"Jack", Age:20}]
    people.GroupBy(t=>t.Age>20 ? {AgeGroup:'Young'} : {AgeGroup:'Old'}, (key, group)=>({Key:key, Names:[...group]}), ageGroupEqualityComparer).ToArray()
    // [{"Key":{"AgeGroup":"Old"},"Names":[{"Name":"Jack","Age":18},{"Name":"Jack","Age":20}]},{"Key":{"AgeGroup":"Young"},"Names":[{"Name":"Joe","Age":22}]}]
    
  • Groups the elements of a sequence according to a specified key selector function and compares the keys by using a specified comparer.

    Type parameters

    • TKey

    Parameters

    • keySelector: (item: T) => TKey

      A function to extract the key for each element.

        • (item: T): TKey
        • Parameters

          • item: T

          Returns TKey

    • Optional comparer: IEqualityComparer<TKey>

      An IEqualityComparer to compare keys.

    Returns IEnumerable<IGrouping<TKey, T>>

    An IEnumerable<IGrouping<TKey, TElement>> where each IGrouping<TKey,TElement> object contains a collection of objects of type TElement and a key.

    let ageGroupEqualityComparer = {
    Equals: (x, y) => x.AgeGroup===y.AgeGroup,
    GetHashCode: obj => obj.AgeGroup
    }
    let people = [{Name:"Jack", Age:18}, {Name:"Joe", Age:22}, {Name:"Jack", Age:20}]
    people.GroupBy(t=>t.Age>20 ? {AgeGroup:'Young'} : {AgeGroup:'Old'}, ageGroupEqualityComparer).Select(t=>({Key:t.Key, Names:t.ToArray()})).ToArray()
    // [{"Key":{"AgeGroup":"Old"},"Names":[{"Name":"Jack","Age":18},{"Name":"Jack","Age":20}]},{"Key":{"AgeGroup":"Young"},"Names":[{"Name":"Joe","Age":22}]}]
    

GroupJoin

  • GroupJoin<TInner, TKey, TResult>(inner: IEnumerable<TInner>, outerKeySelector: (outerItem: T) => TKey, innerKeySelector: (innerItem: TInner) => TKey, resultSelector: (outerItem: T, inner: IEnumerable<TInner>) => TResult, comparer?: IEqualityComparer<TKey>): IEnumerable<TResult>
  • Correlates the elements of two sequences based on key equality and groups the results. A specified IEqualityComparer<T> is used to compare keys.

    Type parameters

    • TInner

    • TKey

    • TResult

    Parameters

    • inner: IEnumerable<TInner>

      The sequence to join to the first sequence.

    • outerKeySelector: (outerItem: T) => TKey

      A function to extract the join key from each element of the first sequence.

        • (outerItem: T): TKey
        • Parameters

          • outerItem: T

          Returns TKey

    • innerKeySelector: (innerItem: TInner) => TKey

      A function to extract the join key from each element of the second sequence.

        • (innerItem: TInner): TKey
        • Parameters

          • innerItem: TInner

          Returns TKey

    • resultSelector: (outerItem: T, inner: IEnumerable<TInner>) => TResult

      A function to create a result element from an element from the first sequence and a collection of matching elements from the second sequence.

        • Parameters

          Returns TResult

    • Optional comparer: IEqualityComparer<TKey>

      An IEqualityComparer to hash and compare keys.

    Returns IEnumerable<TResult>

    An IEnumerable<T> that contains elements of type TResult that are obtained by performing a grouped join on two sequences.

    let nameComparer = {
    Equals: (a,b) => a.Name===b.Name,
    GetHashCode: a => a.Name
    }
    let people = [{Name:"Hedlund, Magnus"},{Name:"Adams, Terry"},{Name:"Weiss, Charlotte"}]
    let pets = [{Name:"Barley", Owner:{Name:"Adams, Terry"}},
    {Name:"Boots", Owner:{Name:"Adams, Terry"}},
    {Name:"Whiskers", Owner:{Name:"Weiss, Charlotte"}},
    {Name:"Daisy", Owner:{Name:"Hedlund, Magnus"}},]
    
    let res = people.GroupJoin(pets, person => person, pet=>pet.Owner,
    (person, petCollection) => ({
    OwnerName: person.Name,
    Pets: [petCollection.Select(pet => pet.Name).Aggregate((seed,item)=>seed+" "+item).trim()]
    }),
    nameComparer)
    .ToArray()
    // [{"OwnerName":"Hedlund, Magnus","Pets":["Daisy"]},{"OwnerName":"Adams, Terry","Pets":["Barley Boots"]},{"OwnerName":"Weiss, Charlotte","Pets":["Whiskers"]}]
    

Intersect

  • Produces the set intersection of two sequences.

    Parameters

    • second: IEnumerable<T>

      An IEnumerable<T> whose distinct elements that also appear in the first sequence will be returned.

    • Optional comparer: IEqualityComparer<T>

      An IEqualityComparer to compare values.

    Returns IEnumerable<T>

    A sequence that contains the elements that form the set intersection of two sequences.

    let nameComparer = {
    Equals: (a,b) => a.Name===b.Name,
    GetHashCode: a => a.Name
    }
    let people = [{Name:"Jack", Age:18}, {Name:"Joe", Age:22}, {Name:"Jack", Age:20}]
    people.Intersect([{Name:"Joe", Age:50}, {Name:"Jane", Age:24}], nameComparer).ToArray()
    // [{"Name":"Joe","Age":22}]
    

IntersectWith

  • Modifies the current HashSet<T> object to contain only elements that are present in that object and in the specified collection.

    Parameters

    • other: IEnumerable<T>

      The collection to compare to the current HashSet<T> object.

      let hs1 = new HashSet([1,2,3,4,5])
      let hs2 = new HashSet([2,3,4])
      hs1.IntersectWith(hs2)
      hs1.ToArray()
      // [2, 3, 4]
      

    Returns void

IsProperSubsetOf

  • Determines whether a HashSet<T> object is a proper subset of the specified collection.

    Parameters

    • other: IEnumerable<T>

      The collection to compare to the current HashSet<T> object.

    Returns boolean

    true if the HashSet<T> object is a proper subset of other; otherwise, false.

    let hs1 = new HashSet([1,2,3,4,5])
    let hs2 = new HashSet([2,3,4])
    let hs3 = new HashSet([2,3,4])
    hs2.IsProperSubsetOf(hs1)
    // true
    hs2.IsProperSubsetOf(hs3)
    // false
    

IsProperSupersetOf

  • Determines whether a HashSet<T> object is a proper superset of the specified collection.

    Parameters

    • other: IEnumerable<T>

      The collection to compare to the current HashSet object.

    Returns boolean

    true if the HashSet<T> object is a proper superset of other; otherwise, false.

    let hs1 = new HashSet([1,2,3,4,5])
    let hs2 = new HashSet([2,3,4])
    let hs3 = new HashSet([2,3,4])
    hs1.IsProperSupersetOf(hs2)
    // true
    hs2.IsProperSupersetOf(hs3)
    // false
    

IsSubsetOf

  • Determines whether a HashSet<T> object is a subset of the specified collection.

    Parameters

    • other: IEnumerable<T>

      The collection to compare to the current HashSet<T> object.

    Returns boolean

    true if the HashSet<T> object is a subset of other; otherwise, false.

    let hs1 = new HashSet([1,2,3,4,5])
    let hs2 = new HashSet([2,3,4])
    let hs3 = new HashSet([2,3,4])
    hs2.IsSubsetOf(hs1)
    // true
    hs1.IsSubsetOf(hs2)
    // false
    hs2.IsSubsetOf(hs3)
    // true
    

IsSupersetOf

  • Determines whether a HashSet<T> object is a superset of the specified collection.

    Parameters

    • other: IEnumerable<T>

      The collection to compare to the current HashSet<T> object.

    Returns boolean

    true if the HashSet<T> object is a superset of other; otherwise, false.

    let hs1 = new HashSet([1,2,3,4,5])
    let hs2 = new HashSet([2,3,4])
    let hs3 = new HashSet([2,3,4])
    hs2.IsSupersetOf(hs1)
    // false
    hs1.IsSupersetOf(hs2)
    // true
    hs2.IsSupersetOf(hs3)
    // true
    

Join

  • Join<TInner, TKey, TResult>(inner: IEnumerable<TInner>, outerKeySelector: (outerItem: T) => TKey, innerKeySelector: (innerItem: TInner) => TKey, resultSelector: (outerItem: T, innerItem: TInner) => TResult, comparer?: IEqualityComparer<TKey>): IEnumerable<TResult>
  • Correlates the elements of two sequences based on matching keys. A specified IEqualityComparer<T> is used to compare keys.

    Type parameters

    • TInner

    • TKey

    • TResult

    Parameters

    • inner: IEnumerable<TInner>

      The sequence to join to the first sequence.

    • outerKeySelector: (outerItem: T) => TKey

      A function to extract the join key from each element of the first sequence.

        • (outerItem: T): TKey
        • Parameters

          • outerItem: T

          Returns TKey

    • innerKeySelector: (innerItem: TInner) => TKey

      A function to extract the join key from each element of the second sequence.

        • (innerItem: TInner): TKey
        • Parameters

          • innerItem: TInner

          Returns TKey

    • resultSelector: (outerItem: T, innerItem: TInner) => TResult

      A function to create a result element from two matching elements.

        • (outerItem: T, innerItem: TInner): TResult
        • Parameters

          • outerItem: T
          • innerItem: TInner

          Returns TResult

    • Optional comparer: IEqualityComparer<TKey>

      An IEqualityComparer<T> to hash and compare keys.

    Returns IEnumerable<TResult>

    An IEnumerable<T> that has elements of type TResult that are obtained by performing an inner join on two sequences.

    let nameComparer = {
    Equals: (a,b) => a.Name===b.Name,
    GetHashCode: a => a.Name
    }
    let people = [{Name:"Hedlund, Magnus"},{Name:"Adams, Terry"},{Name:"Weiss, Charlotte"},{Name:"Without Pets"}]
    let pets = [{Name:"Barley", Owner:{Name:"Adams, Terry"}},
    {Name:"Boots", Owner:{Name:"Adams, Terry"}},
    {Name:"Pet Without Owner", Owner:{Name:"Doe, Jane"}},
    {Name:"Whiskers", Owner:{Name:"Weiss, Charlotte"}},
    {Name:"Daisy", Owner:{Name:"Hedlund, Magnus"}},];
    
    people.Join(pets, person => person, pet=>pet.Owner,
    (person, pet) => ({
    OwnerName: person.Name,
    Pet: pet.Name
    }),
    nameComparer)
    .ToArray();
    // [{"OwnerName":"Hedlund, Magnus","Pet":"Daisy"},{"OwnerName":"Adams, Terry","Pet":"Barley"},{"OwnerName":"Adams, Terry","Pet":"Boots"},{"OwnerName":"Weiss, Charlotte","Pet":"Whiskers"}]
    

Last

  • Last(predicate?: (item: T) => boolean): T
  • Returns the last element of a sequence that satisfies a specified condition.

    Parameters

    • Optional predicate: (item: T) => boolean

      A function to test each element for a condition.

        • (item: T): boolean
        • Parameters

          • item: T

          Returns boolean

    Returns T

    The last element in the sequence that passes the test in the specified predicate function.

    [1,1,2,2,2,3,4,5,6,6].Last()
    // 6
    [1,1,2,2,2,3,4,5,6,6].Last(t=>t<3)
    // 2
    [].Last()
    // Error - Uncaught No last element
    

LastOrDefault

  • LastOrDefault(predicate?: (item: T) => boolean, defaultValue?: T): T
  • Returns the last element of a sequence that satisfies a condition or a default value if no such element is found.

    Parameters

    • Optional predicate: (item: T) => boolean

      A function to test each element for a condition.

        • (item: T): boolean
        • Parameters

          • item: T

          Returns boolean

    • Optional defaultValue: T

      Default value if no element is found.

    Returns T

    Default value if the sequence is empty or if no elements pass the test in the predicate function; otherwise, the last element that passes the test in the predicate function.

    [1,1,2,2,2,3,4,5,6,6].LastOrDefault(t=>true, 123)
    // 6
    [1,1,2,2,2,3,4,5,6,6].LastOrDefault(t=>t<3, 123)
    // 2
    [].LastOrDefault(t=>t<3, 123)
    // 123
    

Max

  • Max(selector?: (item: T) => number): number
  • Invokes a transform function on each element of a sequence and returns the maximum Single value.

    Parameters

    • Optional selector: (item: T) => number

      A transform function to apply to each element.

        • (item: T): number
        • Parameters

          • item: T

          Returns number

    Returns number

    The maximum value in the sequence.

    [1,2,6,4,7,34,7,8].Max()
    // 34
    let people = [{Name:"Jack", Age:18}, {Name:"Joe", Age:22}, {Name:"Jack", Age:20}]
    people.Max(t => t.Age)
    // 22
    

Min

  • Min(selector?: (item: T) => number): number
  • Invokes a transform function on each element of a sequence and returns the minimum resulting value.

    Parameters

    • Optional selector: (item: T) => number

      A transform function to apply to each element.

        • (item: T): number
        • Parameters

          • item: T

          Returns number

    Returns number

    The minimum value in the sequence.

    [1,2,6,4,7,34,7,8].Min()
    // 1
    let people = [{Name:"Jack", Age:18}, {Name:"Joe", Age:22}, {Name:"Jack", Age:20}]
    people.Min(t => t.Age)
    // 18
    

OfType

  • Filters the elements of an IEnumerable based on a specified type.

    Type parameters

    • TResult

    Parameters

    • type: string

      The type to filter the elements of the sequence on.

    Returns IEnumerable<TResult>

    An IEnumerable<T> that contains elements from the input sequence of type TResult.

    [4,1,"1",2,3,"3",4,5,2, true].OfType("string").ToArray()
    // ["1", "3"]
    [4,1,"1",2,3,"3",4,5,2, true].OfType("number").ToArray()
    // [4, 1, 2, 3, 4, 5, 2]
    [4,1,"1",2,3,"3",4,5,2, true].OfType("boolean").ToArray()
    [4,1,"1",true, new Set([1,2])].OfType("Set").ToArray()
    // Set {1, 2}
    

OrderBy

  • Sorts the elements of a sequence in ascending order.

    Type parameters

    • TKey

    Parameters

    • keySelector: (item: T) => TKey

      A function to extract a key from an element.

        • (item: T): TKey
        • Parameters

          • item: T

          Returns TKey

    • Optional comparer: IComparer<TKey>

      An IComparer<TKey> to compare keys.

    Returns IOrderedEnumerable<T>

    An IOrderedEnumerable<T> whose elements are sorted in ascending according to a key.

    ["Jane", "Doe", "Joe"].OrderBy(t=>t).ToArray()
    // ["Doe", "Jane", "Joe"]
    

OrderByDescending

  • Sorts the elements of a sequence in descending order.

    Type parameters

    • TKey

    Parameters

    • keySelector: (item: T) => TKey

      A function to extract a key from an element.

        • (item: T): TKey
        • Parameters

          • item: T

          Returns TKey

    • Optional comparer: IComparer<TKey>

      An IComparer<TKey> to compare keys.

    Returns IOrderedEnumerable<T>

    An IOrderedEnumerable<T> whose elements are sorted in descending order according to a key.

    ["Jane", "Doe", "Joe"].OrderByDescending(t=>t).ToArray()
    // ["Joe", "Jane", "Doe"]
    

Overlaps

  • Determines whether the current HashSet<T> object and a specified collection share common elements.

    Parameters

    • other: IEnumerable<T>

      The collection to compare to the current HashSet<T> object.

    Returns boolean

    true if the HashSet<T> object and other share at least one common element; otherwise, false.

    let hs1 = new HashSet([1,2,3,4,5])
    let hs2 = new HashSet([6,7,8])
    let hs3 = new HashSet([3,6,7,8])
    hs1.Overlaps(hs2)
    // false
    hs1.Overlaps(hs3)
    // true
    hs2.Overlaps(hs3)
    // true
    

Prepend

  • Adds a value to the beginning of the sequence.

    Parameters

    • element: T

      The value to prepend to source.

    Returns IEnumerable<T>

    A new sequence that begins with element.

    [1,9,15].Prepend(20).ToArray()
    // [20, 1, 9, 15]
    

Remove

  • Remove(item: T): boolean
  • Removes the specified element from a HashSet<T> object.

    Parameters

    • item: T

      The element to remove.

    Returns boolean

    true if the element is successfully found and removed; otherwise, false. This method returns false if item is not found in the HashSet<T> object.

    let hs1 = new HashSet([1,2,3,4,5])
    hs1.Remove(3)
    // true
    hs1.Remove(15)
    // false
    hs1.ToArray()
    // [1, 2, 4, 5]
    

RemoveWhere

  • RemoveWhere(match: (item: T) => boolean): number
  • Removes all elements that match the conditions defined by the specified predicate from a HashSet<T> collection.

    Parameters

    • match: (item: T) => boolean

      The function that defines the conditions of the elements to remove.

        • (item: T): boolean
        • Parameters

          • item: T

          Returns boolean

    Returns number

    The number of elements that were removed from the HashSet<T> collection.

    let hs1 = new HashSet([1,2,3,4,5])
    hs1.RemoveWhere(t=>t%2==0)
    // 2
    [...hs1]
    // [1, 3, 5]
    

Reverse

  • Inverts the order of the elements in a sequence.

    Returns IEnumerable<T>

    A sequence whose elements correspond to those of the input sequence in reverse order.

    [1,9,15].Reverse().ToArray()
    // [15, 9, 1]
    

Select

  • Select<TResult>(selector: (item: T, index?: number) => TResult): IEnumerable<TResult>
  • Projects each element of a sequence into a new form by incorporating the element's index.

    Type parameters

    • TResult

    Parameters

    • selector: (item: T, index?: number) => TResult

      A transform function to apply to each source element; the second parameter of the function represents the index of the source element.

        • (item: T, index?: number): TResult
        • Parameters

          • item: T
          • Optional index: number

          Returns TResult

    Returns IEnumerable<TResult>

    An IEnumerable<T> whose elements are the result of invoking the transform function on each element of source.

    [1,2,3].Select(t=>t*2).ToArray()
    // [2, 4, 6]
    [1,2,3].Select((t,i)=>i+" => "+t).ToArray()
    // ["0 => 1", "1 => 2", "2 => 3"]
    

SelectMany

  • SelectMany<TCollection, TResult>(collectionSelector: (list: T, index?: number) => IEnumerable<TCollection>, resultSelector: (list: T, item: TCollection) => TResult): IEnumerable<TResult>
  • SelectMany<TResult>(collectionSelector: (list: T, index?: number) => IEnumerable<TResult>): IEnumerable<TResult>
  • Projects each element of a sequence to an IEnumerable<T>, flattens the resulting sequences into one sequence, and invokes a result selector function on each element therein. The index of each source element is used in the intermediate projected form of that element.

    Type parameters

    • TCollection

    • TResult

    Parameters

    • collectionSelector: (list: T, index?: number) => IEnumerable<TCollection>

      A transform function to apply to each source element; the second parameter of the function represents the index of the source element.

        • Parameters

          • list: T
          • Optional index: number

          Returns IEnumerable<TCollection>

    • resultSelector: (list: T, item: TCollection) => TResult

      A transform function to apply to each element of the intermediate sequence.

        • (list: T, item: TCollection): TResult
        • Parameters

          • list: T
          • item: TCollection

          Returns TResult

    Returns IEnumerable<TResult>

    An IEnumerable<TResult> whose elements are the result of invoking the one-to-many transform function collectionSelector on each element of source and then mapping each of those sequence elements and their corresponding source element to a result element.

    let people = [
    { Name:"Jane", Items: [ "a", "b" ] },
    { Name:"Jack", Items: [ "c", "d" ] },
    { Name:"Joe", Items: [ "e", "f" ] },
    { Name:"John", Items: [ "g" ] },
    ]
    
    people.SelectMany((t,i) => t.Items.Select((z)=>z+i), (per, item) => ({ name:per.Name, item })).ToArray();
    // [{"name":"Jane","item":"a0"},{"name":"Jane","item":"b0"},{"name":"Jack","item":"c1"},
    //  {"name":"Jack","item":"d1"},{"name":"Joe","item":"e2"},{"name":"Joe","item":"f2"},
    //  {"name":"John","item":"g3"}]
    
  • Projects each element of a sequence to an IEnumerable<T>, and flattens the resulting sequences into one sequence. The index of each source element is used in the projected form of that element.

    Type parameters

    • TResult

    Parameters

    • collectionSelector: (list: T, index?: number) => IEnumerable<TResult>

      A transform function to apply to each source element; the second parameter of the function represents the index of the source element.

        • Parameters

          • list: T
          • Optional index: number

          Returns IEnumerable<TResult>

    Returns IEnumerable<TResult>

    An IEnumerable<T> whose elements are the result of invoking the one-to-many transform function on each element of an input sequence.

    let people = [
    { Name:"Jane", Items: [ "a", "b" ] },
    { Name:"Jack", Items: [ "c", "d" ] },
    { Name:"Joe", Items: [ "e", "f" ] },
    { Name:"John", Items: [ "g" ] },
    ]
    
    people.SelectMany((t,i) => t.Items.Select((z)=>z+i)).ToArray();
    // ["a0", "b0", "c1", "d1", "e2", "f2", "g3"]
    

SequenceEqual

  • Determines whether two sequences are equal by comparing their elements by using a specified IEqualityComparer<T>.

    Parameters

    • second: IEnumerable<T>

      An IEnumerable<T> to compare to the first sequence.

    • Optional comparer: IEqualityComparer<T>

      An IEqualityComparer<T> to use to compare elements.

    Returns boolean

    true if the two source sequences are of equal length and their corresponding elements compare equal according to comparer; otherwise, false.

    let nameComparer = {
    Equals: (a,b) => a.Name===b.Name,
    GetHashCode: a => a.Name
    }
    let people = [{Name:"Jack", Age:18}, {Name:"Joe", Age:22}, {Name:"Jack", Age:20}]
    people.SequenceEqual([{Name:"Jack"}, {Name:"Joe"}, {Name:"Jack"}], nameComparer)
    // true
    people.SequenceEqual([{Name:"Jack"}, {Name:"Jack"}], nameComparer)
    // false
    

SetEquals

  • Determines whether a HashSet<T> object and the specified collection contain the same elements.

    Parameters

    • other: IEnumerable<T>

      The collection to compare to the current HashSet<T> object.

    Returns boolean

    true if the HashSet<T> object is equal to other; otherwise, false.

    let hs1 = new HashSet([1,2,3,4,5])
    let hs2 = new HashSet([2,3,4])
    let hs3 = new HashSet([2,4,3])
    hs1.SetEquals(hs2)
    // false
    hs2.SetEquals(hs3)
    // true
    

Single

  • Single(predicate?: (item: T) => boolean): T
  • Returns the only element of a sequence that satisfies a specified condition, and throws an exception if more than one such element exists.

    Parameters

    • Optional predicate: (item: T) => boolean

      A function to test an element for a condition.

        • (item: T): boolean
        • Parameters

          • item: T

          Returns boolean

    Returns T

    The single element of the input sequence that satisfies a condition.

    [1,2,3,4].Single(t=>t>3)
    // 4
    [1,2,3,4].Single(t=>t>2)
    // Error - Uncaught More than 1 element
    [1].Single()
    // 1
    [1,2].Single()
    // Error - Uncaught More than 1 element
    [].Single()
    // Error - Uncaught No element
    

SingleOrDefault

  • SingleOrDefault(predicate?: (item: T) => boolean, defaultValue?: T): T
  • Returns the only element of a sequence that satisfies a specified condition or a default value if no such element exists; this method throws an exception if more than one element satisfies the condition.

    Parameters

    • Optional predicate: (item: T) => boolean

      A function to test an element for a condition.

        • (item: T): boolean
        • Parameters

          • item: T

          Returns boolean

    • Optional defaultValue: T

      Default value if no element is found.

    Returns T

    The single element of the input sequence that satisfies the condition, or default value if no such element is found.

    [1,2,3,4].SingleOrDefault(t=>t>3, 123)
    // 4
    [1,2,3,4].SingleOrDefault(t=>t>2, 123)
    // Error - Uncaught More than 1 element
    [1].SingleOrDefault()
    // 1
    [1].SingleOrDefault(t=>true, 123)
    // 1
    [1,2].SingleOrDefault()
    // Error - Uncaught More than 1 element
    [].SingleOrDefault(t=>true, 123)
    // 123
    [].SingleOrDefault()
    // null
    

Skip

  • Bypasses a specified number of elements in a sequence and then returns the remaining elements.

    Parameters

    • count: number

      The number of elements to skip before returning the remaining elements.

    Returns IEnumerable<T>

    An IEnumerable<T> that contains the elements that occur after the specified index in the input sequence.

    [1,2,3,4,5].Skip(4).ToArray()
    // [5]
    

SkipLast

  • Skip a specified number of elements at the end of a sequence.

    Parameters

    • count: number

      The number of elements to skip at the end of a sequence.

    Returns IEnumerable<T>

    An IEnumerable<T> that contains the elements that occur at the beginning of a sequence without a specified number of elements at the end.

    [1,2,3,4,5].SkipLast(2).ToArray()
    // [1, 2, 3]
    

SkipWhile

  • SkipWhile(predicate: (item: T, index?: number) => boolean): IEnumerable<T>
  • Bypasses elements in a sequence as long as a specified condition is true and then returns the remaining elements. The element's index is used in the logic of the predicate function.

    Parameters

    • predicate: (item: T, index?: number) => boolean

      A function to test each source element for a condition; the second parameter of the function represents the index of the source element.

        • (item: T, index?: number): boolean
        • Parameters

          • item: T
          • Optional index: number

          Returns boolean

    Returns IEnumerable<T>

    An IEnumerable<T> that contains the elements from the input sequence starting at the first element in the linear series that does not pass the test specified by predicate.

    [1,2,3,4,5].SkipWhile((t,i) => i < 3).ToArray()
    // [4, 5]
    [1,2,3,4,5].SkipWhile((t) => t < 3).ToArray()
    // [3, 4, 5]
    

Sum

  • Sum(selector?: (item: T) => number): number
  • Computes the sum of the sequence of numeric values that are obtained by invoking a transform function on each element of the input sequence.

    Parameters

    • Optional selector: (item: T) => number

      A transform function to apply to each element.

        • (item: T): number
        • Parameters

          • item: T

          Returns number

    Returns number

    The sum of the projected values.

    [1,2,3,4,5].Sum()
    // 15
    let people = [{Name:"Jack", Age:18}, {Name:"Joe", Age:22}, {Name:"Jack", Age:20}]
    people.Sum(t=>t.Age)
    // 60
    

SymmetricExceptWith

  • Modifies the current HashSet<T> object to contain only elements that are present either in that object or in the specified collection, but not both.

    Parameters

    • other: IEnumerable<T>

      The collection to compare to the current HashSet<T> object.

      let hs1 = new HashSet([1,2,3,4,5])
      let hs2 = new HashSet([2,3,4,5,6])
      hs1.SymmetricExceptWith(hs2)
      [...hs1]
      // [1, 6]
      

    Returns void

Take

  • Returns a specified number of contiguous elements from the start of a sequence.

    Parameters

    • count: number

      The number of elements to return.

    Returns IEnumerable<T>

    An IEnumerable<T> that contains the specified number of elements from the start of the input sequence.

    [1,2,3,4,5].Take(3).ToArray()
    // [1, 2, 3]
    

TakeLast

  • Take a specified number of elements at the end of a sequence.

    Parameters

    • count: number

      The number of elements to return.

    Returns IEnumerable<T>

    An IEnumerable<T> that contains the specified number of elements from the end of the input sequence.

    [1,2,3,4,5].TakeLast(3).ToArray()
    // [3, 4, 5]
    

TakeWhile

  • TakeWhile(predicate: (item: T, index?: number) => boolean): IEnumerable<T>
  • Returns elements from a sequence as long as a specified condition is true. The element's index is used in the logic of the predicate function.

    Parameters

    • predicate: (item: T, index?: number) => boolean

      A function to test each source element for a condition; the second parameter of the function represents the index of the source element.

        • (item: T, index?: number): boolean
        • Parameters

          • item: T
          • Optional index: number

          Returns boolean

    Returns IEnumerable<T>

    An IEnumerable<T> that contains elements from the input sequence that occur before the element at which the test no longer passes.

    [1,5,9,12,45,6].TakeWhile((t,i)=>i<5).ToArray()
    // [1, 5, 9, 12, 45]
    [1,5,9,12,45,6].TakeWhile((t)=>t<12).ToArray()
    // [1, 5, 9]
    

ToArray

  • ToArray(): T[]
  • Creates an array from a IEnumerable<T>.

    Returns T[]

    An array that contains the elements from the input sequence.

    new Set([1,5,9,8]).ToArray()
    // [1, 5, 9, 8]
    

ToDictionary

  • ToDictionary<TKey, TValue>(keySelector: (item: T) => TKey, elementSelector?: (item: T) => TValue, comparer?: IEqualityComparer<T>): Dictionary<TKey, TValue>
  • ToDictionary<TKey, TValue>(keySelector: (item: T) => TKey, comparer: IEqualityComparer<T>): Dictionary<TKey, TValue>
  • Creates a Dictionary<TKey,TValue> from an IEnumerable<T> according to a specified key selector function, a comparer, and an element selector function.

    Type parameters

    • TKey

    • TValue

    Parameters

    • keySelector: (item: T) => TKey

      A function to extract a key from each element.

        • (item: T): TKey
        • Parameters

          • item: T

          Returns TKey

    • Optional elementSelector: (item: T) => TValue

      A transform function to produce a result element value from each element.

        • (item: T): TValue
        • Parameters

          • item: T

          Returns TValue

    • Optional comparer: IEqualityComparer<T>

      An IEqualityComparer<T> to compare keys.

    Returns Dictionary<TKey, TValue>

    A Dictionary<TKey,TValue> that contains values of type TValue selected from the input sequence.

    let nameComparer = {
    Equals: (a,b) => a.Name===b.Name,
    GetHashCode: a => a.Name
    }
    let people = [{Name:"Jack", Age:18}, {Name:"Joe", Age:22}, {Name:"Jane", Age:20}]
    people.ToDictionary(t=>({Name:t.Name}), t=>t.Age, nameComparer).ToArray()
    // [{"Key":{"Name":"Jack"},"Value":18},{"Key":{"Name":"Joe"},"Value":22},{"Key":{"Name":"Jane"},"Value":20}]
    people.ToDictionary(t=>t.Name, t=>t.Age).ToArray()
    // [{"Key":"Jack","Value":18},{"Key":"Joe","Value":22},{"Key":"Jane","Value":20}]
    
  • Creates a Dictionary<TKey,TValue> from an IEnumerable<T> according to a specified key selector function and key comparer.

    Type parameters

    • TKey

    • TValue

    Parameters

    • keySelector: (item: T) => TKey

      A function to extract a key from each element.

        • (item: T): TKey
        • Parameters

          • item: T

          Returns TKey

    • comparer: IEqualityComparer<T>

      An IEqualityComparer<T> to compare keys.

    Returns Dictionary<TKey, TValue>

    A Dictionary<TKey,TValue> that contains keys and values.

    let nameComparer = {
    Equals: (a,b) => a.Name===b.Name,
    GetHashCode: a => a.Name
    }
    let people = [{Name:"Jack", Age:18}, {Name:"Joe", Age:22}, {Name:"Jane", Age:20}]
    people.ToDictionary(t=>({Name:t.Name}), nameComparer).ToArray()
    // [{"Key":{"Name":"Jack"},"Value":{"Name":"Jack","Age":18}},{"Key":{"Name":"Joe"},"Value":{"Name":"Joe","Age":22}},{"Key":{"Name":"Jane"},"Value":{"Name":"Jane","Age":20}}]
    people.ToDictionary(t=>t.Name).ToArray()
    // [{"Key":"Jack","Value":{"Name":"Jack","Age":18}},{"Key":"Joe","Value":{"Name":"Joe","Age":22}},{"Key":"Jane","Value":{"Name":"Jane","Age":20}}]
    

ToHashSet

  • Creates a HashSet<T> from an IEnumerable<T> using the comparer to compare keys.

    Parameters

    Returns HashSet<T>

    A HashSet<T> that contains values of type T selected from the input sequence.

    let nameComparer = {
    Equals: (a,b) => a.Name===b.Name,
    GetHashCode: a => a.Name
    }
    let people = [{Name:"Jack", Age:18}, {Name:"Joe", Age:22}, {Name:"Jack", Age:20}]
    people.ToHashSet(nameComparer).ToArray()
    // [{"Name":"Jack","Age":18},{"Name":"Joe","Age":22}]
    

ToList

  • Creates a List<T> from an IEnumerable<T>.

    Parameters

    • Optional comparer: IComparer<T>

      An IComparer<TKey> to compare keys.

    Returns List<T>

    A List<T> that contains elements from the input sequence.

    let people = [{Name:"Jack", Age:18}, {Name:"Joe", Age:22}, {Name:"Jack", Age:20}]
    let mylist = people.ToList()
    mylist.Add({Name:"Jane", Age:19})
    mylist.ToArray()
    // [{Name:"Jack", Age:18}, {Name:"Joe", Age:22}, {Name:"Jack", Age:20}, {Name:"Jane", Age:19}]
    

ToLookup

  • ToLookup<TKey, TElement>(keySelector: (item: T) => TKey, elementSelector?: (item: T) => TElement, comparer?: IEqualityComparer<T>): Lookup<TKey, TElement>
  • ToLookup<TKey, TElement>(keySelector: (item: T) => TKey, comparer: IEqualityComparer<T>): Lookup<TKey, TElement>
  • Creates a Lookup<TKey,TElement> from an IEnumerable<T> according to a specified key selector function, a comparer and an element selector function.

    Type parameters

    • TKey

    • TElement

    Parameters

    • keySelector: (item: T) => TKey

      A function to extract a key from each element.

        • (item: T): TKey
        • Parameters

          • item: T

          Returns TKey

    • Optional elementSelector: (item: T) => TElement

      A transform function to produce a result element value from each element.

        • (item: T): TElement
        • Parameters

          • item: T

          Returns TElement

    • Optional comparer: IEqualityComparer<T>

      An IEqualityComparer<T> to compare keys.

    Returns Lookup<TKey, TElement>

    A Lookup<TKey,TElement> that contains values of type TElement selected from the input sequence.

    let nameComparer = {
    Equals: (a,b) => a.Name===b.Name,
    GetHashCode: a => a.Name
    }
    let people = [{Name:"Jack", Age:18}, {Name:"Joe", Age:22}, {Name:"Jack", Age:20}]
    people.ToLookup(t=>({Name:t.Name}), t=>t.Age, nameComparer).Select(t=>({Name:t.Key.Name, Ages:t.ToArray()})).ToArray()
    // [{"Name":"Jack","Ages":[18,20]},{"Name":"Joe","Ages":[22]}]
    people.ToLookup(t=>t.Name, t=>t.Age).Select(t=>({Name:t.Key, Ages:t.ToArray()})).ToArray()
    // [{"Name":"Jack","Ages":[18,20]},{"Name":"Joe","Ages":[22]}]
    
  • Creates a Lookup<TKey,TElement> from an IEnumerable<T> according to a specified key selector function and key comparer.

    Type parameters

    • TKey

    • TElement

    Parameters

    • keySelector: (item: T) => TKey

      A function to extract a key from each element.

        • (item: T): TKey
        • Parameters

          • item: T

          Returns TKey

    • comparer: IEqualityComparer<T>

      An IEqualityComparer<T> to compare keys.

    Returns Lookup<TKey, TElement>

    A Lookup<TKey,TElement> that contains keys and values.

    let nameComparer = {
    Equals: (a,b) => a.Name===b.Name,
    GetHashCode: a => a.Name
    }
    let people = [{Name:"Jack", Age:18}, {Name:"Joe", Age:22}, {Name:"Jack", Age:20}]
    people.ToLookup(t=>({Name:t.Name}), nameComparer).Select(t=>({Name:t.Key.Name, Ages:t.ToArray()})).ToArray()
    // [{"Name":"Jack","Ages":[{"Name":"Jack","Age":18},{"Name":"Jack","Age":20}]},{"Name":"Joe","Ages":[{"Name":"Joe","Age":22}]}]
    people.ToLookup(t=>t.Name).Select(t=>({Name:t.Key, Ages:t.ToArray()})).ToArray()
    // [{"Name":"Jack","Ages":[{"Name":"Jack","Age":18},{"Name":"Jack","Age":20}]},{"Name":"Joe","Ages":[{"Name":"Joe","Age":22}]}]
    

ToMap

  • ToMap<TKey, TElement>(keySelector: (item: T) => TKey, elementSelector?: (item: T) => TElement): Map<TKey, TElement>
  • Creates a Map<TKey,TElement> from an IEnumerable<T> according to a specified key selector function and an element selector function.

    Type parameters

    • TKey

    • TElement

    Parameters

    • keySelector: (item: T) => TKey

      A function to extract a key from each element.

        • (item: T): TKey
        • Parameters

          • item: T

          Returns TKey

    • Optional elementSelector: (item: T) => TElement

      A transform function to produce a result element value from each element.

        • (item: T): TElement
        • Parameters

          • item: T

          Returns TElement

    Returns Map<TKey, TElement>

    A Map<TKey,TElement> that contains values of type TElement selected from the input sequence.

    let people = [{Name:"Jack", Age:18}, {Name:"Joe", Age:22}, {Name:"Jane", Age:20}]
    people.ToMap(t=>t.Name, t=>t.Age)
    // Map {"Jack" => 18, "Joe" => 22, "Jane" => 20}
    people.ToMap(t=>t.Name)
    // Map {"Jack" => {"Name":"Jack","Age":18}, "Joe" => {"Name":"Joe","Age":22}, "Jane" => {"Name":"Jane","Age":20}}
    

ToSet

  • ToSet(): Set<T>
  • Creates a Set<T> from an IEnumerable<T>.

    Returns Set<T>

    A HashSet<T> that contains values of type T selected from the input sequence.

    [1,2,5,3,2,5,1].ToSet()
    // Set {1, 2, 5, 3}
    

TryGetValue

  • TryGetValue(equalValue: T): { actualValue: T; contains: boolean }
  • Searches the set for a given value and returns the equal value it finds, if any.

    Parameters

    • equalValue: T

      The value to search for.

    Returns { actualValue: T; contains: boolean }

    An object with properties actualValue and contains indicating whether the search was successful.

    let hs1 = new HashSet([1,2,3,4,5])
    hs1.TryGetValue(2)
    // {actualValue: 2, contains: true}
    hs1.TryGetValue(6)
    // {actualValue: undefined, contains: false}
    
    • actualValue: T
    • contains: boolean

Union

  • Produces the set union of two sequences by using a specified IEqualityComparer<T>.

    Parameters

    • second: IEnumerable<T>

      An IEnumerable<T> whose distinct elements form the second set for the union.

    • Optional comparer: IEqualityComparer<T>

      The IEqualityComparer<T> to compare values.

    Returns IEnumerable<T>

    An IEnumerable<T> that contains the elements from both input sequences, excluding duplicates.

    [1,2,5,2].Union([3,4,5]).ToArray()
    // [1, 2, 5, 3, 4]
    let nameComparer = {
    Equals: (a,b) => a.Name===b.Name,
    GetHashCode: a => a.Name
    }
    let people = [{Name:"Jack", Age:18}, {Name:"Joe", Age:22}, {Name:"Jack", Age:20}]
    people.Union([{Name:"Jack", Age:35}, {Name:"Jane", Age:23}], nameComparer).ToArray()
    // [{"Name":"Jack","Age":18},{"Name":"Joe","Age":22},{"Name":"Jane","Age":23}]
    

UnionWith

  • Modifies the current HashSet<T> object to contain all elements that are present in itself, the specified collection, or both.

    Parameters

    • other: IEnumerable<T>

      The collection to compare to the current HashSet<T> object.

      let hs1 = new HashSet([1,2,3,4,5])
      let hs2 = new HashSet([2,3,4,5,6])
      hs1.UnionWith(hs2)
      [...hs1]
      // [1, 2, 3, 4, 5, 6]
      

    Returns void

Where

  • Where(predicate: (item: T, index?: number) => boolean): IEnumerable<T>
  • Filters a sequence of values based on a predicate.

    Parameters

    • predicate: (item: T, index?: number) => boolean

      A function to test each source element for a condition; the second parameter of the function represents the index of the source element.

        • (item: T, index?: number): boolean
        • Parameters

          • item: T
          • Optional index: number

          Returns boolean

    Returns IEnumerable<T>

    An IEnumerable<T> that contains elements from the input sequence that satisfy the condition.

    [1,5,6,4,3,2,1,8,9].Where((t,i)=>t>3 && i>2).ToArray()
    // [4, 8, 9]
    

Zip

  • Zip<TSecond, TResult>(second: IEnumerable<TSecond>, resultSelector: (first: T, second: TSecond) => TResult): IEnumerable<TResult>
  • Applies a specified function to the corresponding elements of two sequences, producing a sequence of the results.

    Type parameters

    • TSecond

    • TResult

    Parameters

    • second: IEnumerable<TSecond>

      The second sequence to merge.

    • resultSelector: (first: T, second: TSecond) => TResult

      A function that specifies how to merge the elements from the two sequences.

        • (first: T, second: TSecond): TResult
        • Parameters

          • first: T
          • second: TSecond

          Returns TResult

    Returns IEnumerable<TResult>

    An IEnumerable<T> that contains merged elements of two input sequences.

    let names = ["Jack", "Jane", "Joe"]
    let ages = [20, 22, 25]
    names.Zip(ages, (Name, Age)=>({Name, Age})).ToArray()
    // [{"Name":"Jack","Age":20},{"Name":"Jane","Age":22},{"Name":"Joe","Age":25}]
    

[Symbol.iterator]

  • [Symbol.iterator](): IterableIterator<T>
  • Returns an enumerator that iterates through a collection.

    Returns IterableIterator<T>

Generated using TypeDoc