Options
All
  • Public
  • Public/Protected
  • All
Menu

Class Dictionary<TKey, TValue>

Represents a collection of keys and values.

Type parameters

  • TKey

  • TValue

Hierarchy

Index

Constructors

constructor

  • Initializes a new instance of the Dictionary<TKey,TValue> class that is empty, and uses the default equality comparer for the key type.

    let dict1 = new Dictionary()
    dict1.Add(1,"Jack")
    dict1.Add(2,"Joe")
    dict1.ToArray()
    // [{"Key":1,"Value":"Jack"},{"Key":2,"Value":"Joe"}]
    

    Type parameters

    • TKey

    • TValue

    Returns Dictionary<TKey, TValue>

  • Initializes a new instance of the Dictionary<TKey,TValue> class that contains elements copied from the specified IEnumerable<KeyValuePair<TKey, TValue>> and uses the specified IEqualityComparer<T>.

    Type parameters

    • TKey

    • TValue

    Parameters

    • dictionary: IEnumerable<KeyValuePair<TKey, TValue>>

      The IEnumerable<KeyValuePair<TKey, TValue>> whose elements are copied to the new Dictionary<TKey,TValue>.

    • Optional comparer: IEqualityComparer<TKey>

      The IEqualityComparer<T> implementation to use when comparing keys, or null to use the default equality comparer.

      let nameComparer = {
      Equals: (a,b) => a.Name===b.Name,
      GetHashCode: a => a.Name
      }
      let dict1 = new Dictionary([{Key:{Name:"Jack"}, Value:20}, {Key:{Name:"John"}, Value:20}], nameComparer)
      

    Returns Dictionary<TKey, TValue>

  • Initializes a new instance of the Dictionary<TKey,TValue> class that is empty, and uses the specified IEqualityComparer<T>.

    Type parameters

    • TKey

    • TValue

    Parameters

    • comparer: IEqualityComparer<TKey>

      The IEqualityComparer<T> implementation to use when comparing keys, or null to use the default Eequality comparer.

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

    Returns Dictionary<TKey, TValue>

Properties

Readonly Comparer

Comparer: IEqualityComparer<TKey>

Gets the IEqualityComparer<T> that is used to determine equality of keys for the dictionary.

Readonly CountNative

CountNative: number

Gets the number of key/value pairs contained in the Dictionary<TKey,TValue>.

let dict1 = new Dictionary()
dict1.Add(1,"Jack")
dict1.Add(2,"Joe")
dict1.CountNative
// 2

Readonly Keys

Keys: KeyCollection<TKey, TValue>

Gets a collection containing the keys in the Dictionary<TKey,TValue>.

let dict1 = new Dictionary()
let keys = dict1.Keys
dict1.Add(1,"Jack")
dict1.Add(2,"Joe")
[...keys]
// [1, 2]

Readonly Values

Values: ValueCollection<TKey, TValue>

Gets a collection containing the values in the Dictionary<TKey,TValue>.

let dict1 = new Dictionary()
let values = dict1.Values
dict1.Add(1,"Jack")
dict1.Add(2,"Joe")
[...values]
// ["Jack", "Joe"]

Methods

Add

  • Add(key: TKey, value: TValue): void
  • Adds the specified key and value to the dictionary. The method throws an Error when attempting to add a duplicate key.

    Parameters

    • key: TKey

      The key of the element to add.

    • value: TValue

      The value of the element to add.

      let dict1 = new Dictionary()
      dict1.Add(1,"Jack")
      dict1.ToArray()
      // [{"Key":1,"Value":"Jack"}]
      

    Returns void

Aggregate

  • Aggregate<TAccumulate, TResult>(seed: TAccumulate, func: (seed: TAccumulate, item: KeyValuePair<TKey, TValue>) => TAccumulate, resultSelector?: (accumulate: TAccumulate) => TResult): TResult
  • Aggregate(func: (seed: KeyValuePair<TKey, TValue>, item: KeyValuePair<TKey, TValue>) => KeyValuePair<TKey, TValue>): KeyValuePair<TKey, TValue>
  • 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: KeyValuePair<TKey, TValue>) => TAccumulate

      An accumulator function to be invoked on each element.

        • (seed: TAccumulate, item: KeyValuePair<TKey, TValue>): TAccumulate
        • Parameters

          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

    Returns KeyValuePair<TKey, TValue>

    The final accumulator value.

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

All

  • All(predicate: (item: KeyValuePair<TKey, TValue>) => boolean): boolean
  • Determines whether all elements of a sequence satisfy a condition.

    Parameters

    • predicate: (item: KeyValuePair<TKey, TValue>) => boolean

      A function to test each element for a condition.

    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: KeyValuePair<TKey, TValue>) => boolean): boolean
  • Determines whether any element of a sequence exists or satisfies a condition.

    Parameters

    • Optional predicate: (item: KeyValuePair<TKey, TValue>) => boolean

      A function to test each element for a condition.

    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: KeyValuePair<TKey, TValue>

      The value to append to source.

    Returns IEnumerable<KeyValuePair<TKey, TValue>>

    A new sequence that ends with element.

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

AsEnumerable

Average

  • Average(selector?: (item: KeyValuePair<TKey, TValue>) => number): number
  • Computes the average of a sequence of values.

    Parameters

    • Optional selector: (item: KeyValuePair<TKey, TValue>) => number

      A transform function to apply to each element.

    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 keys and values from the Dictionary<TKey,TValue>.

    let dict1 = new Dictionary()
    dict1.Add(1,"Jack")
    dict1.Clear()
    dict1.ToArray()
    // []
    

    Returns void

Concat

  • Concatenates two sequences.

    Parameters

    Returns IEnumerable<KeyValuePair<TKey, TValue>>

    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

    Returns boolean

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

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

ContainsKey

  • ContainsKey(key: TKey): boolean
  • Determines whether the Dictionary<TKey,TValue> contains the specified key.

    Parameters

    • key: TKey

      The key to locate in the Dictionary<TKey,TValue>.

    Returns boolean

    true if the Dictionary<TKey,TValue> contains an element with the specified key; otherwise, false.

    let dict1 = new Dictionary()
    dict1.Add(1,"Jack")
    dict1.ContainsKey(1)
    // true
    dict1.ContainsKey(2)
    // false
    

ContainsValue

  • ContainsValue(value: TValue): boolean
  • Determines whether the Dictionary<TKey,TValue> contains a specific value.

    Parameters

    • value: TValue

      The value to locate in the Dictionary<TKey,TValue>.

    Returns boolean

    true if the Dictionary<TKey,TValue> contains an element with the specified value; otherwise, false.

    let dict1 = new Dictionary()
    dict1.Add(1,"Jack")
    dict1.ContainsValue("Jack")
    // true
    dict1.ContainsValue("Joe")
    // false
    

Count

  • Count(predicate?: (item: KeyValuePair<TKey, TValue>) => boolean): number
  • Returns the number of elements in a sequence.

    Parameters

    • Optional predicate: (item: KeyValuePair<TKey, TValue>) => boolean

      A function to test each element for a condition.

    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
    

Custom

  • Wrapper for custom projection or filtering.

    Type parameters

    • K

    Parameters

    • fun: (source: IEnumerable<KeyValuePair<TKey, TValue>>) => () => IterableIterator<K>

      A generator function which yields transformed source.

    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: KeyValuePair<TKey, TValue>

      The value to return if the sequence is empty.

    Returns IEnumerable<KeyValuePair<TKey, TValue>>

    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<KeyValuePair<TKey, TValue>>

    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

  • Returns the element at a specified index in a sequence.

    Parameters

    • index: number

      The zero-based index of the element to retrieve.

    Returns KeyValuePair<TKey, TValue>

    The element at the specified position in the source sequence.

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

ElementAtOrDefault

  • 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: KeyValuePair<TKey, TValue>

      Default value if the index is out of range.

    Returns KeyValuePair<TKey, TValue>

    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<KeyValuePair<TKey, TValue>>

      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<KeyValuePair<TKey, TValue>>

      An IEqualityComparer<T> to compare values.

    Returns IEnumerable<KeyValuePair<TKey, TValue>>

    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]
    

First

  • Returns the first element of a sequence.

    Parameters

    • Optional predicate: (item: KeyValuePair<TKey, TValue>) => boolean

      A function to test each element for a condition.

    Returns KeyValuePair<TKey, TValue>

    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

  • Returns the first element of a sequence, or a default value if no element is found.

    Parameters

    • Optional predicate: (item: KeyValuePair<TKey, TValue>) => boolean

      A function to test each element for a condition.

    • Optional defaultValue: KeyValuePair<TKey, TValue>

      Default value if no element is found.

    Returns KeyValuePair<TKey, TValue>

    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: KeyValuePair<TKey, TValue>, index?: number) => void): void
  • Executes callback function on every element of a sequence.

    Parameters

    • callback: (item: KeyValuePair<TKey, TValue>, index?: number) => void

      Function to be executed on every element of a sequence.

      [1,3,5].ForEach(t=>console.log(t))
      // 1
      // 3
      // 5
      
        • Parameters

          Returns void

    Returns void

Get

  • Get(key: TKey): TValue
  • Gets the value associated with the specified key.

    Parameters

    • key: TKey

      The key of the value to get.

    Returns TValue

    The value associated with the specified key.

    let dict1 = new Dictionary()
    dict1.Add(1,"Jack")
    dict1.Get(1)
    // "Jack"
    

GroupBy

  • 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: KeyValuePair<TKey, TValue>) => TKey

      A function to extract the key for each element.

    • elementSelector: (item: KeyValuePair<TKey, TValue>) => TElement

      A function to map each source element to an element in an IGrouping<TKey,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: KeyValuePair<TKey, TValue>) => TKey

      A function to extract the key for each element.

    • elementSelector: (item: KeyValuePair<TKey, TValue>) => TElement

      A function to map each source element to an element in the IGrouping<TKey,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: KeyValuePair<TKey, TValue>) => TKey

      A function to extract the key for each element.

    • elementSelector: (item: KeyValuePair<TKey, TValue>) => TElement

      A function to map each source element to an element in an IGrouping<TKey,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

    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: KeyValuePair<TKey, TValue>) => TKey

      A function to extract the key for each element.

    • Optional comparer: IEqualityComparer<TKey>

      An IEqualityComparer to compare keys.

    Returns IEnumerable<IGrouping<TKey, KeyValuePair<TKey, TValue>>>

    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

  • 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: KeyValuePair<TKey, TValue>) => TKey

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

    • 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: KeyValuePair<TKey, TValue>, 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.

    • 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

    Returns IEnumerable<KeyValuePair<TKey, TValue>>

    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}]
    

Join

  • Join<TInner, TKey, TResult>(inner: IEnumerable<TInner>, outerKeySelector: (outerItem: KeyValuePair<TKey, TValue>) => TKey, innerKeySelector: (innerItem: TInner) => TKey, resultSelector: (outerItem: KeyValuePair<TKey, TValue>, 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: KeyValuePair<TKey, TValue>) => TKey

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

    • 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: KeyValuePair<TKey, TValue>, innerItem: TInner) => TResult

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

        • (outerItem: KeyValuePair<TKey, TValue>, innerItem: TInner): TResult
        • Parameters

          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

  • Returns the last element of a sequence that satisfies a specified condition.

    Parameters

    • Optional predicate: (item: KeyValuePair<TKey, TValue>) => boolean

      A function to test each element for a condition.

    Returns KeyValuePair<TKey, TValue>

    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

  • 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: KeyValuePair<TKey, TValue>) => boolean

      A function to test each element for a condition.

    • Optional defaultValue: KeyValuePair<TKey, TValue>

      Default value if no element is found.

    Returns KeyValuePair<TKey, TValue>

    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: KeyValuePair<TKey, TValue>) => number): number
  • Invokes a transform function on each element of a sequence and returns the maximum Single value.

    Parameters

    • Optional selector: (item: KeyValuePair<TKey, TValue>) => number

      A transform function to apply to each element.

    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: KeyValuePair<TKey, TValue>) => number): number
  • Invokes a transform function on each element of a sequence and returns the minimum resulting value.

    Parameters

    • Optional selector: (item: KeyValuePair<TKey, TValue>) => number

      A transform function to apply to each element.

    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: KeyValuePair<TKey, TValue>) => TKey

      A function to extract a key from an element.

    • Optional comparer: IComparer<TKey>

      An IComparer<TKey> to compare keys.

    Returns IOrderedEnumerable<KeyValuePair<TKey, TValue>>

    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: KeyValuePair<TKey, TValue>) => TKey

      A function to extract a key from an element.

    • Optional comparer: IComparer<TKey>

      An IComparer<TKey> to compare keys.

    Returns IOrderedEnumerable<KeyValuePair<TKey, TValue>>

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

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

Prepend

  • Adds a value to the beginning of the sequence.

    Parameters

    • element: KeyValuePair<TKey, TValue>

      The value to prepend to source.

    Returns IEnumerable<KeyValuePair<TKey, TValue>>

    A new sequence that begins with element.

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

Remove

  • Remove(key: TKey): boolean
  • Removes the value with the specified key from the Dictionary<TKey,TValue>.

    Parameters

    • key: TKey

      The key of the element to remove.

    Returns boolean

    true if the element is successfully found and removed; otherwise, false. This method returns false if key is not found in the Dictionary<TKey,TValue>.

    let dict1 = new Dictionary()
    dict1.Add(1,"Jack")
    dict1.Add(2,"Joe")
    dict1.Remove(1)
    // true
    dict1.Remove(3)
    // false
    dict1.ToArray()
    // [{"Key":2,"Value":"Joe"}]
    

Reverse

  • Inverts the order of the elements in a sequence.

    Returns IEnumerable<KeyValuePair<TKey, TValue>>

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

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

Select

  • Projects each element of a sequence into a new form by incorporating the element's index.

    Type parameters

    • TResult

    Parameters

    • selector: (item: KeyValuePair<TKey, TValue>, 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: KeyValuePair<TKey, TValue>, index?: number): TResult
        • Parameters

          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

  • 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: KeyValuePair<TKey, TValue>, 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.

    • resultSelector: (list: KeyValuePair<TKey, TValue>, item: TCollection) => TResult

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

        • (list: KeyValuePair<TKey, TValue>, item: TCollection): TResult
        • Parameters

          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: KeyValuePair<TKey, TValue>, 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.

    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

    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
    

Set

  • Set(key: TKey, value: TValue): void
  • Sets the value associated with the specified key. Override the value if the key exists.

    Parameters

    • key: TKey

      The key of the value to set.

    • value: TValue

      The value to set.

      let dict1 = new Dictionary()
      dict1.Add(1,"Jack")
      dict1.Set(1,"Joe")
      dict1.Set(2,"Jane")
      dict1.ToArray()
      // [{"Key":1,"Value":"Joe"},{"Key":2,"Value":"Jane"}]
      

    Returns void

Single

  • 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: KeyValuePair<TKey, TValue>) => boolean

      A function to test an element for a condition.

    Returns KeyValuePair<TKey, TValue>

    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

  • 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: KeyValuePair<TKey, TValue>) => boolean

      A function to test an element for a condition.

    • Optional defaultValue: KeyValuePair<TKey, TValue>

      Default value if no element is found.

    Returns KeyValuePair<TKey, TValue>

    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<KeyValuePair<TKey, TValue>>

    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<KeyValuePair<TKey, TValue>>

    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

  • 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: KeyValuePair<TKey, TValue>, 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: KeyValuePair<TKey, TValue>, index?: number): boolean
        • Parameters

          Returns boolean

    Returns IEnumerable<KeyValuePair<TKey, TValue>>

    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: KeyValuePair<TKey, TValue>) => 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: KeyValuePair<TKey, TValue>) => number

      A transform function to apply to each element.

    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
    

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<KeyValuePair<TKey, TValue>>

    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<KeyValuePair<TKey, TValue>>

    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

  • 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: KeyValuePair<TKey, TValue>, 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: KeyValuePair<TKey, TValue>, index?: number): boolean
        • Parameters

          Returns boolean

    Returns IEnumerable<KeyValuePair<TKey, TValue>>

    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

  • Creates an array from a IEnumerable<T>.

    Returns KeyValuePair<TKey, TValue>[]

    An array that contains the elements from the input sequence.

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

ToDictionary

  • 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: KeyValuePair<TKey, TValue>) => TKey

      A function to extract a key from each element.

    • Optional elementSelector: (item: KeyValuePair<TKey, TValue>) => TValue

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

    • Optional comparer: IEqualityComparer<KeyValuePair<TKey, TValue>>

      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

    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<KeyValuePair<TKey, TValue>>

    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

    Returns List<KeyValuePair<TKey, TValue>>

    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

  • 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: KeyValuePair<TKey, TValue>) => TKey

      A function to extract a key from each element.

    • Optional elementSelector: (item: KeyValuePair<TKey, TValue>) => TElement

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

    • Optional comparer: IEqualityComparer<KeyValuePair<TKey, TValue>>

      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

    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: KeyValuePair<TKey, TValue>) => TKey, elementSelector?: (item: KeyValuePair<TKey, TValue>) => 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: KeyValuePair<TKey, TValue>) => TKey

      A function to extract a key from each element.

    • Optional elementSelector: (item: KeyValuePair<TKey, TValue>) => TElement

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

    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

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

    Returns Set<KeyValuePair<TKey, TValue>>

    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}
    

TryAdd

  • TryAdd(key: TKey, value: TValue): boolean
  • Try to add the specified key and value to the dictionary. The method returns false when attempting to add a duplicate key.

    Parameters

    • key: TKey

      The key of the element to add.

    • value: TValue

      The value of the element to add.

    Returns boolean

    true if key and value are successfully added and false if key is already present.

    let dict1 = new Dictionary()
    dict1.TryAdd(1,"Jack")
    // true
    dict1.TryAdd(1,"Joe")
    // false
    

TryGetValue

  • TryGetValue(key: TKey): { contains: boolean; value: TValue }
  • Gets the value associated with the specified key.

    Parameters

    • key: TKey

      The key of the value to get.

    Returns { contains: boolean; value: TValue }

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

    let dict1 = new Dictionary()
    dict1.Add(1,"Jack")
    dict1.TryGetValue(1)
    // {value: "Jack", contains: true}
    dict1.TryGetValue(2)
    // {value: undefined, contains: false}
    
    • contains: boolean
    • value: TValue

Union

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

    Parameters

    Returns IEnumerable<KeyValuePair<TKey, TValue>>

    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}]
    

Where

  • Filters a sequence of values based on a predicate.

    Parameters

    • predicate: (item: KeyValuePair<TKey, TValue>, 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: KeyValuePair<TKey, TValue>, index?: number): boolean
        • Parameters

          Returns boolean

    Returns IEnumerable<KeyValuePair<TKey, TValue>>

    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

  • 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: KeyValuePair<TKey, TValue>, second: TSecond) => TResult

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

        • (first: KeyValuePair<TKey, TValue>, second: TSecond): TResult
        • Parameters

          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<KeyValuePair<TKey, TValue>>

Generated using TypeDoc