Options
All
  • Public
  • Public/Protected
  • All
Menu

Class List<T>

Represents a list of objects that can be accessed by index. Provides methods to search, sort, and manipulate lists.

Type parameters

  • T

Hierarchy

Index

Constructors

constructor

  • Initializes a new instance of the List<T> class that is empty and has the default initial capacity.

    Type parameters

    • T

    Parameters

    • Optional comparer: IComparer<T>

      An IComparer<TKey> to compare items.

      let mylist = new List()
      mylist.Add({Name:"Jack", Age:30})
      mylist.ToArray()
      // [{"Name":"Jack","Age":30}]
      

    Returns List<T>

  • Initializes a new instance of the List<T> class that contains elements copied from the specified collection.

    Type parameters

    • T

    Parameters

    • collection: IEnumerable<T>

      The collection whose elements are copied to the new list.

    • Optional comparer: IComparer<T>

      An IComparer<TKey> to compare items.

      let mylist = new List([{"Name":"Jane","Age":20}])
      mylist.Add({Name:"Jack", Age:30})
      mylist.ToArray()
      // [{"Name":"Jane","Age":20},{"Name":"Jack","Age":30}]
      

    Returns List<T>

Properties

Readonly Comparer

Comparer: IComparer<T>

Gets the IComparer<T> that is used to compare items.

Readonly CountNative

CountNative: number

Gets the number of elements contained in the List<T>.

let mylist = new List([{"Name":"Jane","Age":20}])
mylist.Add({Name:"Jack", Age:30})
mylist.CountNative
// 2

Methods

Add

  • Add(item: T): void
  • Adds an object to the end of the List<T>.

    Parameters

    • item: T

      The object to be added to the end of the List<T>.

      let mylist = new List()
      mylist.Add({Name:"Jack", Age:30})
      mylist.ToArray()
      // [{"Name":"Jack","Age":30}]
      

    Returns void

AddRange

  • Adds the elements of the specified collection to the end of the List<T>.

    Parameters

    • collection: IEnumerable<T>

      The collection whose elements should be added to the end of the List<T>.

      let mylist = new List()
      mylist.AddRange([{"Name":"Jane","Age":20},{"Name":"Jack","Age":30}])
      mylist.ToArray()
      // [{"Name":"Jane","Age":20},{"Name":"Jack","Age":30}]
      

    Returns void

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
    

BinarySearch

  • BinarySearch(item: T, comparer?: IComparer<T>): number
  • BinarySearch(index: number, count: number, item: T, comparer: IComparer<T>): number
  • Searches the entire sorted List<T> for an element using the specified comparer and returns the zero-based index of the element.

    Parameters

    • item: T

      The object to locate.

    • Optional comparer: IComparer<T>

      The IComparer<T> implementation to use when comparing elements.

    Returns number

    The zero-based index of item in the sorted List<T>, if item is found; otherwise, a negative number that is the bitwise complement of the index of the next element that is larger than item or, if there is no larger element, the bitwise complement of Count.

    let ageComparer = (a, b) => (a.Age > b.Age ? 1 : a.Age < b.Age ? -1 : 0)
    let mylist = new List()
    mylist.Add({Name:"Jack", Age:10})
    mylist.Add({Name:"Jane", Age:20})
    mylist.Add({Name:"Joe", Age:30})
    mylist.Add({Name:"John", Age:40})
    mylist.BinarySearch({Age:30}, ageComparer)
    // 2
    let person = {Name: "Doe", Age:25}
    let index = mylist.BinarySearch(person, ageComparer)
    index
    // -3
    mylist.Insert(~index, person)
    mylist.ToArray()
    // [{"Name":"Jack","Age":10},{"Name":"Jane","Age":20},
    //  {"Name":"Doe","Age":25},{"Name":"Joe","Age":30},
    //  {"Name":"John","Age":40}]
    
  • Searches a range of elements in the sorted List<T> for an element using the specified comparer and returns the zero-based index of the element.

    Parameters

    • index: number

      The zero-based starting index of the range to search.

    • count: number

      The length of the range to search.

    • item: T

      The object to locate.

    • comparer: IComparer<T>

      The IComparer<T> implementation to use when comparing elements.

    Returns number

    The zero-based index of item in the sorted List<T>, if item is found; otherwise, a negative number that is the bitwise complement of the index of the next element that is larger than item or, if there is no larger element, the bitwise complement of Count.

    let ageComparer = (a, b) => (a.Age > b.Age ? 1 : a.Age < b.Age ? -1 : 0)
    let mylist = new List()
    mylist.Add({Name:"Jack", Age:10})
    mylist.Add({Name:"Jane", Age:20})
    mylist.Add({Name:"Joe", Age:30})
    mylist.Add({Name:"John", Age:40})
    mylist.BinarySearch({Age:30}, ageComparer)
    // 2
    let person = {Name: "Doe", Age:25}
    let index = mylist.BinarySearch(1, 2, person, ageComparer)
    index
    // -3
    mylist.Insert(~index, person)
    mylist.ToArray()
    // [{"Name":"Jack","Age":10},{"Name":"Jane","Age":20},
    //  {"Name":"Doe","Age":25},{"Name":"Joe","Age":30},
    //  {"Name":"John","Age":40}]
    

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 the List<T>.

    let mylist = new List()
    mylist.Add({Name:"Jack", Age:30})
    mylist.Clear()
    mylist.ToArray()
    // []
    

    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 an element is in the List<T>.

    Parameters

    • item: T

      The object to locate in the List.

    Returns boolean

    true if item is found in the List<T>; otherwise, false.

    let ageComparer = (a, b) => (a.Age > b.Age ? 1 : a.Age < b.Age ? -1 : 0)
    let mylist = new List(ageComparer)
    mylist.Add({Name:"Jack", Age:10})
    mylist.Add({Name:"Jane", Age:20})
    mylist.Add({Name:"Joe", Age:30})
    mylist.Add({Name:"John", Age:40})
    mylist.ContainsNative({Age:20})
    // true
    mylist.ContainsNative({Age:25})
    // false
    

ConvertAll

  • ConvertAll<TOutput>(converter: (item: T) => TOutput): List<TOutput>
  • Converts the elements in the current List<T> to another type, and returns a list containing the converted elements.

    Type parameters

    • TOutput

    Parameters

    • converter: (item: T) => TOutput

      A function that converts each element from one type to another type.

        • (item: T): TOutput
        • Parameters

          • item: T

          Returns TOutput

    Returns List<TOutput>

    A List<T> of the target type containing the converted elements from the current List<T>.

    let mylist = new List()
    mylist.Add({Name:"Jack", Age:10})
    mylist.Add({Name:"Jane", Age:20})
    let newlist = mylist.ConvertAll(t => ({Name:t.Name, Age:t.Age+1}))
    mylist.ToArray()
    // [{"Name":"Jack","Age":10},{"Name":"Jane","Age":20}]
    newlist.ToArray()
    // [{"Name":"Jack","Age":11},{"Name":"Jane","Age":21}]
    

CopyTo

  • CopyTo(array: T[], arrayIndex?: number): void
  • CopyTo(index: number, array: T[], arrayIndex: number, count: number): void
  • Copies the entire List<T> to a compatible one-dimensional array, starting at the specified index of the target array.

    Parameters

    • array: T[]

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

    • Optional arrayIndex: number

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

      let mylist = new List([1,2,3])
      let arr = [15,16,17,18,19,20]
      mylist.CopyTo(arr, 2)
      arr
      // [15, 16, 1, 2, 3, 20]
      

    Returns void

  • Copies a range of elements from the List<T> to a compatible one-dimensional array, starting at the specified index of the target array.

    Parameters

    • index: number

      The zero-based index in the source List<T> at which copying begins.

    • array: T[]

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

    • arrayIndex: number

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

    • count: number

      The number of elements to copy.

      let mylist = new List([1,2,3,4,5,6,7,8])
      let arr = [15,16,17,18,19,20]
      mylist.CopyTo(1, arr, 2, 3)
      arr
      // [15, 16, 2, 3, 4, 20]
      

    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
    

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]
    

Exists

  • Exists(match: (item: T) => boolean): boolean
  • Determines whether the List<T> contains elements that match the conditions defined by the specified predicate.

    Parameters

    • match: (item: T) => boolean

      The function that defines the conditions of the elements to search for.

        • (item: T): boolean
        • Parameters

          • item: T

          Returns boolean

    Returns boolean

    true if the List<T> contains one or more elements that match the conditions defined by the specified predicate; otherwise, false.

    let mylist = new List()
    mylist.Add({Name:"Jack", Age:10})
    mylist.Add({Name:"Jane", Age:20})
    mylist.Exists(t=>t.Age===20)
    // true
    mylist.Exists(t=>t.Age===25)
    // false
    

Find

  • Find(match: (item: T) => boolean, defaultValue?: T): T
  • Searches for an element that matches the conditions defined by the specified predicate, and returns the first occurrence within the entire List<T>.

    Parameters

    • match: (item: T) => boolean

      The function that defines the conditions of the element to search for.

        • (item: T): boolean
        • Parameters

          • item: T

          Returns boolean

    • Optional defaultValue: T

      Default value if no elements are found.

    Returns T

    The first element that matches the conditions defined by the specified predicate, if found; otherwise, the default value.

    let mylist = new List()
    mylist.Add({Name:"Jack", Age:10})
    mylist.Add({Name:"Jane", Age:20})
    mylist.Find(t=>t.Age===20, {Name:"Andy", Age:45})
    // {"Name":"Jane","Age":20}
    mylist.Find(t=>t.Age===25, {Name:"Andy", Age:45})
    // {"Name":"Andy","Age":45}
    mylist.Find(t=>t.Age===25)
    // null
    

FindAll

  • FindAll(match: (item: T) => boolean): List<T>
  • Retrieves all the elements that match the conditions defined by the specified predicate.

    Parameters

    • match: (item: T) => boolean

      The function that defines the conditions of the elements to search for.

        • (item: T): boolean
        • Parameters

          • item: T

          Returns boolean

    Returns List<T>

    A List<T> containing all the elements that match the conditions defined by the specified predicate, if found; otherwise, an empty List<T>.

    let mylist = new List()
    mylist.Add({Name:"Jack", Age:10})
    mylist.Add({Name:"Jane", Age:20})
    mylist.Add({Name:"Joe", Age:30})
    mylist.FindAll(t=>t.Age>15).ToArray()
    // [{"Name":"Jane","Age":20},{"Name":"Joe","Age":30}]
    mylist.FindAll(t=>t.Age>30).ToArray()
    // []
    

FindIndex

  • FindIndex(match: (item: T) => boolean): number
  • FindIndex(startIndex: number, match: (item: T) => boolean): number
  • FindIndex(startIndex: number, count: number, match: (item: T) => boolean): number
  • Searches for an element that matches the conditions defined by the specified predicate, and returns the zero-based index of the first occurrence within the entire List<T>.

    Parameters

    • match: (item: T) => boolean

      The function that defines the conditions of the element to search for.

        • (item: T): boolean
        • Parameters

          • item: T

          Returns boolean

    Returns number

    The zero-based index of the first occurrence of an element that matches the conditions defined by match, if found; otherwise, -1.

    let mylist = new List()
    mylist.Add({Name:"Jack", Age:10})
    mylist.Add({Name:"Jane", Age:20})
    mylist.Add({Name:"Joe", Age:30})
    mylist.FindIndex(t=>t.Age>15)
    // 1
    mylist.FindIndex(t=>t.Age===30)
    // 2
    mylist.FindIndex(t=>t.Age>30)
    // -1
    
  • Searches for an element that matches the conditions defined by the specified predicate, and returns the zero-based index of the first occurrence within the range of elements in the List<T> that extends from the specified index to the last element.

    Parameters

    • startIndex: number

      The zero-based starting index of the search.

    • match: (item: T) => boolean

      The function that defines the conditions of the element to search for.

        • (item: T): boolean
        • Parameters

          • item: T

          Returns boolean

    Returns number

    The zero-based index of the first occurrence of an element that matches the conditions defined by match, if found; otherwise, -1.

    let mylist = new List()
    mylist.Add({Name:"Jack", Age:10})
    mylist.Add({Name:"Jane", Age:20})
    mylist.Add({Name:"Joe", Age:30})
    mylist.FindIndex(2, t=>t.Age>15)
    // 2
    mylist.FindIndex(1, t=>t.Age===30)
    // 2
    mylist.FindIndex(1, t=>t.Age>30)
    // -1
    
  • Searches for an element that matches the conditions defined by the specified predicate, and returns the zero-based index of the first occurrence within the range of elements in the List<T> that starts at the specified index and contains the specified number of elements.

    Parameters

    • startIndex: number

      The zero-based starting index of the search.

    • count: number

      The number of elements in the section to search.

    • match: (item: T) => boolean

      The function that defines the conditions of the element to search for.

        • (item: T): boolean
        • Parameters

          • item: T

          Returns boolean

    Returns number

    The zero-based index of the first occurrence of an element that matches the conditions defined by match, if found; otherwise, -1.

    let mylist = new List()
    mylist.Add({Name:"Jack", Age:10})
    mylist.Add({Name:"Jane", Age:20})
    mylist.Add({Name:"Joe", Age:30})
    mylist.FindIndex(2, 1, t=>t.Age>15)
    // 2
    mylist.FindIndex(1, 1, t=>t.Age===30)
    // -1
    mylist.FindIndex(1, 2, t=>t.Age>30)
    // -1
    

FindLast

  • FindLast(match: (item: T) => boolean, defaultValue?: T): number
  • Searches for an element that matches the conditions defined by the specified predicate, and returns the last occurrence within the entire List<T>.

    Parameters

    • match: (item: T) => boolean

      The function that defines the conditions of the element to search for.

        • (item: T): boolean
        • Parameters

          • item: T

          Returns boolean

    • Optional defaultValue: T

      Default value if no elements are found.

    Returns number

    The last element that matches the conditions defined by the specified predicate, if found; otherwise, the default value.

    let mylist = new List()
    mylist.Add({Name:"Jack", Age:10})
    mylist.Add({Name:"Jane", Age:20})
    mylist.Add({Name:"Joe", Age:20})
    mylist.FindLast(t=>t.Age===20, {Name:"Andy", Age:45})
    // {"Name":"Joe", "Age":20}
    mylist.FindLast(t=>t.Age===25, {Name:"Andy", Age:45})
    // {"Name":"Andy","Age":45}
    mylist.FindLast(t=>t.Age===25)
    // null
    

FindLastIndex

  • FindLastIndex(match: (item: T) => boolean): number
  • FindLastIndex(startIndex: number, match: (item: T) => boolean): number
  • FindLastIndex(startIndex: number, count: number, match: (item: T) => boolean): number
  • Searches for an element that matches the conditions defined by the specified predicate, and returns the zero-based index of the last occurrence within the entire List<T>.

    Parameters

    • match: (item: T) => boolean

      The function that defines the conditions of the element to search for.

        • (item: T): boolean
        • Parameters

          • item: T

          Returns boolean

    Returns number

    The zero-based index of the last occurrence of an element that matches the conditions defined by match, if found; otherwise, -1.

    let mylist = new List()
    mylist.Add({Name:"Jack", Age:10})
    mylist.Add({Name:"Jane", Age:20})
    mylist.Add({Name:"Joe", Age:20})
    mylist.FindLastIndex(t=>t.Age>5)
    // 2
    mylist.FindLastIndex(t=>t.Age>30)
    // -1
    
  • Searches for an element that matches the conditions defined by the specified predicate, and returns the zero-based index of the last occurrence within the range of elements in the List<T> that extends from the first element to the specified index.

    Parameters

    • startIndex: number

      The zero-based starting index of the backward search.

    • match: (item: T) => boolean

      The function that defines the conditions of the element to search for.

        • (item: T): boolean
        • Parameters

          • item: T

          Returns boolean

    Returns number

    The zero-based index of the last occurrence of an element that matches the conditions defined by match, if found; otherwise, -1.

    let mylist = new List()
    mylist.Add({Name:"Jack", Age:10})
    mylist.Add({Name:"Jane", Age:20})
    mylist.Add({Name:"Joe", Age:20})
    mylist.FindLastIndex(1, t=>t.Age>5)
    // 2
    mylist.FindIndex(1, t=>t.Age==10)
    // -1
    mylist.FindIndex(1, t=>t.Age>30)
    // -1
    
  • Searches for an element that matches the conditions defined by the specified predicate, and returns the zero-based index of the last occurrence within the range of elements in the List<T> that contains the specified number of elements and ends at the specified index.

    Parameters

    • startIndex: number

      The zero-based starting index of the backward search.

    • count: number

      The number of elements in the section to search.

    • match: (item: T) => boolean

      The function that defines the conditions of the element to search for.

        • (item: T): boolean
        • Parameters

          • item: T

          Returns boolean

    Returns number

    The zero-based index of the last occurrence of an element that matches the conditions defined by match, if found; otherwise, -1.

    let mylist = new List()
    mylist.Add({Name:"Jack", Age:10})
    mylist.Add({Name:"Jane", Age:20})
    mylist.Add({Name:"Joe", Age:20})
    mylist.FindIndex(1, 1, t=>t.Age>15)
    // 1
    mylist.FindIndex(2, 1, t=>t.Age>15)
    // 2
    mylist.FindIndex(1, 2, t=>t.Age==10)
    // -1
    

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

Get

  • Get(index: number): T
  • Gets the element at the specified index.

    Parameters

    • index: number

      The zero-based index of the element to get.

    Returns T

    The element at the specified index.

    let mylist = new List([{"Name":"Jane","Age":20}])
    mylist.Add({Name:"Jack", Age:30})
    mylist.Get(0)
    // {"Name":"Jane","Age":20}
    mylist.Get(1)
    // {"Name":"Jack","Age":30}
    

GetRange

  • GetRange(index: number, count: number): List<T>
  • Creates a shallow copy of a range of elements in the source List<T>.

    Parameters

    • index: number

      The zero-based List<T> index at which the range starts.

    • count: number

      The number of elements in the range.

    Returns List<T>

    A shallow copy of a range of elements in the source List<T>.

    let mylist = new List()
    mylist.Add({Name:"Jack", Age:10})
    mylist.Add({Name:"Jane", Age:20})
    mylist.Add({Name:"Joe", Age:20})
    mylist.Add({Name:"John", Age:50})
    let newlist1 = mylist.GetRange(0,2)
    newlist.ToArray()
    // [{"Name":"Jack","Age":10},{"Name":"Jane","Age":20}]
    let newlist2 = mylist.GetRange(1,2)
    newlist.ToArray()
    // [{"Name":"Jane","Age":20},{"Name":"Joe","Age":20}]
    

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

IndexOf

  • IndexOf(item: T, index?: number, count?: number): number
  • Searches for the specified object and returns the zero-based index of the first occurrence within the range of elements in the List<T> that starts at the specified index and contains the specified number of elements.

    Parameters

    • item: T

      The object to locate in the List<T>.

    • Optional index: number

      The zero-based starting index of the search. 0 (zero) is valid in an empty list.

    • Optional count: number

      The number of elements in the section to search.

    Returns number

    The zero-based index of the first occurrence of item within the range of elements in the List<T> that starts at index and contains count number of elements, if found; otherwise, -1.

    let ageComparer = (a, b) => (a.Age > b.Age ? 1 : a.Age < b.Age ? -1 : 0)
    let mylist = new List(ageComparer)
    mylist.Add({Name:"Jack", Age:10})
    mylist.Add({Name:"Jane", Age:20})
    mylist.Add({Name:"Joe", Age:20})
    mylist.Add({Name:"John", Age:50})
    mylist.IndexOf({Age:20})
    // 1
    mylist.IndexOf({Age:20}, 2, 2)
    // 2
    mylist.IndexOf({Age:20}, 3, 1)
    // -1
    

Insert

  • Insert(index: number, item: T): void
  • Inserts an element into the List<T> at the specified index.

    Parameters

    • index: number

      The zero-based index at which item should be inserted.

    • item: T

      The object to insert.

      let mylist = new List()
      mylist.Add({Name:"Jack", Age:10})
      mylist.Add({Name:"Jane", Age:20})
      mylist.Add({Name:"Joe", Age:20})
      mylist.Insert(1, {Name:"John", Age:50})
      mylist.ToArray()
      // [{"Name":"Jack","Age":10},{"Name":"John","Age":50},
      //  {"Name":"Jane","Age":20},{"Name":"Joe","Age":20}]
      

    Returns void

InsertRange

  • InsertRange(index: number, collection: IEnumerable<T>): void
  • nserts the elements of a collection into the List<T> at the specified index.

    Parameters

    • index: number

      The zero-based index at which the new elements should be inserted.

    • collection: IEnumerable<T>

      The collection whose elements should be inserted into the List<T>.

      let mylist = new List()
      mylist.Add({Name:"Jack", Age:10})
      mylist.Add({Name:"Jane", Age:20})
      mylist.Add({Name:"Joe", Age:20})
      mylist.InsertRange(1, [{Name:"John", Age:50}, {Name:"Doe", Age:70}])
      mylist.ToArray()
      // [{"Name":"Jack","Age":10},{"Name":"John","Age":50},
      //  {"Name":"Doe","Age":70},{"Name":"Jane","Age":20},
      //  {"Name":"Joe","Age":20}]
      

    Returns void

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

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
    

LastIndexOf

  • LastIndexOf(item: T, index?: number, count?: number): number
  • Searches for the specified object and returns the zero-based index of the last occurrence within the range of elements in the List<T> that contains the specified number of elements and ends at the specified index.

    Parameters

    • item: T

      The object to locate in the List<T>.

    • Optional index: number

      The zero-based starting index of the backward search.

    • Optional count: number

      The number of elements in the section to search.

    Returns number

    The zero-based index of the last occurrence of item within the range of elements in the List<T> that contains count number of elements and ends at index, if found; otherwise, -1.

    let ageComparer = (a, b) => (a.Age > b.Age ? 1 : a.Age < b.Age ? -1 : 0)
    let mylist = new List(ageComparer)
    mylist.Add({Name:"Jack", Age:10})
    mylist.Add({Name:"Jane", Age:20})
    mylist.Add({Name:"Joe", Age:20})
    mylist.Add({Name:"John", Age:50})
    mylist.LastIndexOf({Age:20})
    // 2
    mylist.LastIndexOf({Age:20}, 0, 2)
    // 1
    mylist.LastIndexOf({Age:20}, 0, 1)
    // -1
    

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

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 first occurrence of a specific object from the List<T>.

    Parameters

    • item: T

      The object to remove from the List<T>. The value can be null for reference types.

    Returns boolean

    true if item is successfully removed; otherwise, false. This method also returns false if item was not found in the List<T>.

    let ageComparer = (a, b) => (a.Age > b.Age ? 1 : a.Age < b.Age ? -1 : 0)
    let mylist = new List(ageComparer)
    mylist.Add({Name:"Jack", Age:10})
    mylist.Add({Name:"Jane", Age:20})
    mylist.Add({Name:"Joe", Age:20})
    mylist.Remove({Age:50})
    // false
    mylist.Remove({Age:20})
    // true
    mylist.ToArray()
    // [{"Name":"Jack","Age":10},{"Name":"Joe","Age":20}]
    

RemoveAll

  • RemoveAll(match: (item: T) => boolean): number
  • Removes all the elements that match the conditions defined by the specified predicate.

    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 removed from the List<T>.

    let mylist = new List()
    mylist.Add({Name:"Jack", Age:10})
    mylist.Add({Name:"Jane", Age:20})
    mylist.Add({Name:"Joe", Age:20})
    mylist.RemoveAll(t=>t.Age>30)
    // 0
    mylist.RemoveAll(t=>t.Age>10)
    // 2
    mylist.ToArray()
    // [{"Name":"Jack","Age":10}]
    

RemoveAt

  • RemoveAt(index: number): void
  • Removes the element at the specified index of the List<T>.

    Parameters

    • index: number

      The zero-based index of the element to remove.

      let mylist = new List()
      mylist.Add({Name:"Jack", Age:10})
      mylist.Add({Name:"Jane", Age:20})
      mylist.Add({Name:"Joe", Age:20})
      mylist.RemoveAt(1)
      mylist.ToArray()
      // [{"Name":"Jack","Age":10},{"Name":"Joe","Age":20}]
      

    Returns void

RemoveRange

  • RemoveRange(index: number, count: number): void
  • Removes a range of elements from the List<T>.

    Parameters

    • index: number

      The zero-based starting index of the range of elements to remove.

    • count: number

      The number of elements to remove.

      let mylist = new List()
      mylist.Add({Name:"Jack", Age:10})
      mylist.Add({Name:"Jane", Age:20})
      mylist.Add({Name:"Joe", Age:20})
      mylist.RemoveRange(1, 2)
      mylist.ToArray()
      // [{"Name":"Jack","Age":10}]
      

    Returns void

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]
    

ReverseNative

  • ReverseNative(): void
  • ReverseNative(index: number, count: number): void
  • Reverses the order of the elements in the entire List<T>.

    let mylist = new List()
    mylist.Add({Name:"Jack", Age:10})
    mylist.Add({Name:"Jane", Age:20})
    mylist.Add({Name:"Joe", Age:20})
    mylist.ReverseNative()
    mylist.ToArray()
    // [{"Name":"Joe","Age":20},{"Name":"Jane","Age":20},
    //  {"Name":"Jack","Age":10}]
    

    Returns void

  • Reverses the order of the elements in the specified range.

    Parameters

    • index: number

      The zero-based starting index of the range to reverse.

    • count: number

      The number of elements in the range to reverse.

      let mylist = new List()
      mylist.Add({Name:"Jack", Age:10})
      mylist.Add({Name:"Jane", Age:20})
      mylist.Add({Name:"Joe", Age:20})
      mylist.ReverseNative(0, 2)
      mylist.ToArray()
      // [{"Name":"Jane","Age":20},{"Name":"Jack","Age":10},
      //  {"Name":"Joe","Age":20}]
      

    Returns void

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
    

Set

  • Set(index: number, item: T): void
  • Sets the element at the specified index.

    Parameters

    • index: number

      The zero-based index of the element to set.

    • item: T

      The element to set.

      let mylist = new List([{"Name":"Jane","Age":20}])
      mylist.Add({Name:"Jack", Age:30})
      mylist.Set(0, {"Name":"Joe","Age":50})
      mylist.Get(0)
      // {"Name":"Joe","Age":50}
      

    Returns void

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]
    

Sort

  • Sort(comparer?: IComparer<T>): void
  • Sort(index: number, count: number, comparer: IComparer<T>): void
  • Sorts the elements in the entire List<T> using the specified comparer.

    Parameters

    • Optional comparer: IComparer<T>

      The IComparer<T> implementation to use when comparing elements.

      let ageComparer = (a, b) => (a.Age > b.Age ? 1 : a.Age < b.Age ? -1 : 0)
      let mylist = new List(ageComparer)
      mylist.Add({Name:"John", Age:20})
      mylist.Add({Name:"Jack", Age:10})
      mylist.Add({Name:"Jane", Age:50})
      mylist.Add({Name:"Joe", Age:20})
      mylist.Sort()
      mylist.ToArray()
      // [{"Name":"Jack","Age":10},{"Name":"John","Age":20},
      //  {"Name":"Joe","Age":20},{"Name":"Jane","Age":50}]
      
      let nameComparer = (a, b) => (a.Name > b.Name ? 1 : a.Name < b.Name ? -1 : 0)
      mylist.Sort(nameComparer)
      mylist.ToArray()
      // [{"Name":"Jack","Age":10},{"Name":"Jane","Age":50},
      //  {"Name":"Joe","Age":20},{"Name":"John","Age":20}]
      

    Returns void

  • Sorts the elements in a range of elements in List<T> using the specified comparer.

    Parameters

    • index: number

      The zero-based starting index of the range to sort.

    • count: number

      The length of the range to sort.

    • comparer: IComparer<T>

      The IComparer<T> implementation to use when comparing elements.

      let ageComparer = (a, b) => (a.Age > b.Age ? 1 : a.Age < b.Age ? -1 : 0)
      let mylist = new List(ageComparer)
      mylist.Add({Name:"John", Age:20})
      mylist.Add({Name:"Jack", Age:10})
      mylist.Add({Name:"Jane", Age:50})
      mylist.Add({Name:"Joe", Age:20})
      mylist.Sort(0, 3, mylist.Comparer)
      mylist.ToArray()
      // [{"Name":"Jack","Age":10},{"Name":"John","Age":20},
      //  {"Name":"Jane","Age":50},{"Name":"Joe","Age":20}]
      

    Returns void

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
    

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}
    

TrueForAll

  • TrueForAll(match: (item: T) => boolean): boolean
  • Determines whether every element in the List<T> matches the conditions defined by the specified predicate.

    Parameters

    • match: (item: T) => boolean

      The function that defines the conditions to check against the elements.

        • (item: T): boolean
        • Parameters

          • item: T

          Returns boolean

    Returns boolean

    true if every element in the List<T> matches the conditions defined by the specified predicate; otherwise, false. If the list has no elements, the return value is true.

    let mylist = new List()
    mylist.Add({Name:"John", Age:20})
    mylist.Add({Name:"Jack", Age:10})
    mylist.Add({Name:"Jane", Age:50})
    mylist.TrueForAll(t=>t.Age>5)
    // true
    mylist.TrueForAll(t=>t.Age>10)
    // false
    

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

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