Initializes a new instance of the List<T>
class that is empty and has the default initial capacity.
An IComparer<TKey>
to compare items.
let mylist = new List()
mylist.Add({Name:"Jack", Age:30})
mylist.ToArray()
// [{"Name":"Jack","Age":30}]
Initializes a new instance of the List<T>
class that contains elements copied from the specified collection.
The collection whose elements are copied to the new list.
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}]
Gets the IComparer<T>
that is used to compare items.
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
Adds an object to the end of the List<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}]
Adds the elements of the specified collection to the end of the List<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}]
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.
The initial accumulator value.
An accumulator function to be invoked on each element.
A function to transform the final accumulator value into the result value.
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.
An accumulator function to be invoked on each element.
The final accumulator value.
["Jane", "Doe", "Joe"].Aggregate((res, item)=>res+" "+item)
// " Jane Doe Joe"
Determines whether all elements of a sequence satisfy a condition.
A function to test each element for a condition.
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
Determines whether any element of a sequence exists or satisfies a condition.
A function to test each element for a condition.
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
Appends a value to the end of the sequence.
The value to append to source.
A new sequence that ends with element.
[1,9,15].Append(20).ToArray()
// [1, 9, 15, 20]
Returns the input typed as IEnumerable<T>
.
The input sequence typed as IEnumerable<T>
.
Computes the average of a sequence of values.
A transform function to apply to each element.
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
Searches the entire sorted List<T>
for an element using the specified comparer and returns the zero-based index of the element.
The object to locate.
The IComparer<T>
implementation to use when comparing elements.
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.
The zero-based starting index of the range to search.
The length of the range to search.
The object to locate.
The IComparer<T>
implementation to use when comparing elements.
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}]
Casts the elements of an IEnumerable to the specified type. Useful for Typescript developers.
An IEnumerable<T>
that contains each element of the source sequence cast to the specified type.
Removes all elements from the List<T>
.
let mylist = new List()
mylist.Add({Name:"Jack", Age:30})
mylist.Clear()
mylist.ToArray()
// []
Concatenates two sequences.
The sequence to concatenate to the first sequence.
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]
Determines whether a sequence contains a specified element.
The value to locate in the sequence.
An IEqualityComparer<T>
to compare values.
true
if the source sequence contains an element that has the specified value; otherwise, false.
[1,9,15].Contains(9)
// true
Determines whether an element is in the List<T>
.
The object to locate in the List
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
Converts the elements in the current List<T>
to another type, and returns a list containing the converted elements.
A function that converts each element from one type to another type.
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}]
Copies the entire List<T>
to a compatible one-dimensional array, starting at the specified index of the target array.
The one-dimensional Array
that is the destination of the elements copied from List<T>
. The Array
must have zero-based indexing.
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]
Copies a range of elements from the List<T>
to a compatible one-dimensional array, starting at the specified index of the target array.
The zero-based index in the source List<T>
at which copying begins.
The one-dimensional Array
that is the destination of the elements copied from List<T>
. The Array must have zero-based indexing.
The zero-based index in array at which copying begins.
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 the number of elements in a sequence.
A function to test each element for a condition.
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
Wrapper for custom projection or filtering.
A generator function which yields transformed source.
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.
Function which aggregates sequence and returns a value.
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}
Returns the elements of an IEnumerable
The value to return if the sequence is empty.
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]
Returns distinct elements from a sequence.
An IEqualityComparer<T>
to compare values.
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]
Returns the element at a specified index in a sequence.
The zero-based index of the element to retrieve.
The element at the specified position in the source sequence.
[1,1,2,2,2,3,4,5,6,6].ElementAt(5)
// 3
Returns the element at a specified index in a sequence or a default value if the index is out of range.
The zero-based index of the element to retrieve.
Default value if the index is out of range.
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
Produces the set difference of two sequences.
An IEnumerable<T>
whose elements that also occur in the first sequence will cause those elements to be removed from the returned sequence.
An IEqualityComparer<T>
to compare values.
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]
Determines whether the List<T>
contains elements that match the conditions defined by the specified predicate.
The function that defines the conditions of the elements to search for.
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
Searches for an element that matches the conditions defined by the specified predicate, and returns the first occurrence within the entire List<T>
.
The function that defines the conditions of the element to search for.
Default value if no elements are found.
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
Retrieves all the elements that match the conditions defined by the specified predicate.
The function that defines the conditions of the elements to search for.
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()
// []
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>
.
The function that defines the conditions of the element to search for.
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.
The zero-based starting index of the search.
The function that defines the conditions of the element to search for.
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.
The zero-based starting index of the search.
The number of elements in the section to search.
The function that defines the conditions of the element to search for.
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
Searches for an element that matches the conditions defined by the specified predicate, and returns the last occurrence within the entire List<T>
.
The function that defines the conditions of the element to search for.
Default value if no elements are found.
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
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>
.
The function that defines the conditions of the element to search for.
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.
The zero-based starting index of the backward search.
The function that defines the conditions of the element to search for.
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.
The zero-based starting index of the backward search.
The number of elements in the section to search.
The function that defines the conditions of the element to search for.
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
Returns the first element of a sequence.
A function to test each element for a condition.
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
Returns the first element of a sequence, or a default value if no element is found.
A function to test each element for a condition.
Default value if no element is found.
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
Executes callback function on every element of a sequence.
Function to be executed on every element of a sequence.
[1,3,5].ForEach(t=>console.log(t))
// 1
// 3
// 5
Gets the element at the specified index.
The zero-based index of the element to get.
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}
Creates a shallow copy of a range of elements in the source List<T>
.
The zero-based List<T>
index at which the range starts.
The number of elements in the range.
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}]
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.
A function to extract the key for each element.
A function to map each source element to an element in an IGrouping<TKey,TElement>
.
A function to create a result value from each group.
An IEqualityComparer<TKey>
to compare keys with.
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.
A function to extract the key for each element.
A function to map each source element to an element in the 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.
A function to extract the key for each element.
A function to map each source element to an element in an IGrouping<TKey,TElement>
.
An IEqualityComparer<T>
to compare keys.
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.
A function to extract the key for each element.
A function to create a result value from each group.
An IEqualityComparer<T>
to compare keys with.
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.
A function to extract the key for each element.
An IEqualityComparer
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}]}]
Correlates the elements of two sequences based on key equality and groups the results. A specified IEqualityComparer<T>
is used to compare keys.
The sequence to join to the first sequence.
A function to extract the join key from each element of the first sequence.
A function to extract the join key from each element of the second sequence.
A function to create a result element from an element from the first sequence and a collection of matching elements from the second sequence.
An IEqualityComparer
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"]}]
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.
The object to locate in the List<T>
.
The zero-based starting index of the search. 0 (zero) is valid in an empty list.
The number of elements in the section to search.
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
Inserts an element into the List<T>
at the specified index.
The zero-based index at which item should be inserted.
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}]
nserts the elements of a collection into the List<T>
at the specified index.
The zero-based index at which the new elements should be inserted.
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}]
Produces the set intersection of two sequences.
An IEnumerable<T>
whose distinct elements that also appear in the first sequence will be returned.
An IEqualityComparer
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}]
Correlates the elements of two sequences based on matching keys. A specified IEqualityComparer<T>
is used to compare keys.
The sequence to join to the first sequence.
A function to extract the join key from each element of the first sequence.
A function to extract the join key from each element of the second sequence.
A function to create a result element from two matching elements.
An IEqualityComparer<T>
to hash and compare keys.
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"}]
Returns the last element of a sequence that satisfies a specified condition.
A function to test each element for a condition.
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
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.
The object to locate in the List<T>
.
The zero-based starting index of the backward search.
The number of elements in the section to search.
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
Returns the last element of a sequence that satisfies a condition or a default value if no such element is found.
A function to test each element for a condition.
Default value if no element is found.
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
Invokes a transform function on each element of a sequence and returns the maximum Single value.
A transform function to apply to each element.
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
Invokes a transform function on each element of a sequence and returns the minimum resulting value.
A transform function to apply to each element.
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
Filters the elements of an IEnumerable
based on a specified type.
The type to filter the elements of the sequence on.
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}
Sorts the elements of a sequence in ascending order.
A function to extract a key from an element.
An IComparer<TKey>
to compare keys.
An IOrderedEnumerable<T>
whose elements are sorted in ascending according to a key.
["Jane", "Doe", "Joe"].OrderBy(t=>t).ToArray()
// ["Doe", "Jane", "Joe"]
Sorts the elements of a sequence in descending order.
A function to extract a key from an element.
An IComparer<TKey>
to compare keys.
An IOrderedEnumerable<T>
whose elements are sorted in descending order according to a key.
["Jane", "Doe", "Joe"].OrderByDescending(t=>t).ToArray()
// ["Joe", "Jane", "Doe"]
Adds a value to the beginning of the sequence.
The value to prepend to source.
A new sequence that begins with element.
[1,9,15].Prepend(20).ToArray()
// [20, 1, 9, 15]
Removes the first occurrence of a specific object from the List<T>
.
The object to remove from the List<T>
. The value can be null for reference types.
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}]
Removes all the elements that match the conditions defined by the specified predicate.
The function that defines the conditions of the elements to remove.
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}]
Removes the element at the specified index of the List<T>
.
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}]
Removes a range of elements from the List<T>
.
The zero-based starting index of the range of elements to remove.
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}]
Inverts the order of the elements in a sequence.
A sequence whose elements correspond to those of the input sequence in reverse order.
[1,9,15].Reverse().ToArray()
// [15, 9, 1]
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}]
Reverses the order of the elements in the specified range.
The zero-based starting index of the range to reverse.
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}]
Projects each element of a sequence into a new form by incorporating the element's index.
A transform function to apply to each source element; the second parameter of the function represents the index of the source element.
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"]
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.
A transform function to apply to each source element; the second parameter of the function represents the index of the source element.
A transform function to apply to each element of the intermediate sequence.
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.
A transform function to apply to each source element; the second parameter of the function represents the index of the source element.
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"]
Determines whether two sequences are equal by comparing their elements by using a specified IEqualityComparer<T>
.
An IEnumerable<T>
to compare to the first sequence.
An IEqualityComparer<T>
to use to compare elements.
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
Sets the element at the specified index.
The zero-based index of the element to set.
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 the only element of a sequence that satisfies a specified condition, and throws an exception if more than one such element exists.
A function to test an element for a condition.
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
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.
A function to test an element for a condition.
Default value if no element is found.
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
Bypasses a specified number of elements in a sequence and then returns the remaining elements.
The number of elements to skip before returning the remaining elements.
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]
Skip a specified number of elements at the end of a sequence.
The number of elements to skip at the end of a sequence.
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]
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.
A function to test each source element for a condition; the second parameter of the function represents the index of the source element.
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]
Sorts the elements in the entire List<T>
using the specified comparer.
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}]
Sorts the elements in a range of elements in List<T>
using the specified comparer.
The zero-based starting index of the range to sort.
The length of the range to sort.
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}]
Computes the sum of the sequence of numeric values that are obtained by invoking a transform function on each element of the input sequence.
A transform function to apply to each element.
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
Returns a specified number of contiguous elements from the start of a sequence.
The number of elements to return.
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]
Take a specified number of elements at the end of a sequence.
The number of elements to return.
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]
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.
A function to test each source element for a condition; the second parameter of the function represents the index of the source element.
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]
Creates an array from a IEnumerable<T>
.
An array that contains the elements from the input sequence.
new Set([1,5,9,8]).ToArray()
// [1, 5, 9, 8]
Creates a Dictionary<TKey,TValue>
from an IEnumerable<T>
according to a specified key selector function, a comparer, and an element selector function.
A function to extract a key from each element.
A transform function to produce a result element value from each element.
An IEqualityComparer<T>
to compare keys.
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.
A function to extract a key from each element.
An IEqualityComparer<T>
to compare keys.
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}}]
Creates a HashSet<T>
from an IEnumerable<T>
using the comparer to compare keys.
An IEqualityComparer
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}]
Creates a List<T>
from an IEnumerable<T>
.
An IComparer<TKey>
to compare keys.
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}]
Creates a Lookup<TKey,TElement>
from an IEnumerable<T>
according to a specified key selector function, a comparer and an element selector function.
A function to extract a key from each element.
A transform function to produce a result element value from each element.
An IEqualityComparer<T>
to compare keys.
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.
A function to extract a key from each element.
An IEqualityComparer<T>
to compare keys.
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}]}]
Creates a Map<TKey,TElement>
from an IEnumerable<T>
according to a specified key selector function and an element selector function.
A function to extract a key from each element.
A transform function to produce a result element value from each element.
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}}
Creates a Set<T>
from an IEnumerable<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}
Determines whether every element in the List<T>
matches the conditions defined by the specified predicate.
The function that defines the conditions to check against the elements.
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
Produces the set union of two sequences by using a specified IEqualityComparer<T>
.
An IEnumerable<T>
whose distinct elements form the second set for the union.
The IEqualityComparer<T>
to compare values.
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}]
Filters a sequence of values based on a predicate.
A function to test each source element for a condition; the second parameter of the function represents the index of the source element.
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]
Applies a specified function to the corresponding elements of two sequences, producing a sequence of the results.
The second sequence to merge.
A function that specifies how to merge the elements from the two sequences.
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}]
Returns an enumerator that iterates through a collection.
Generated using TypeDoc
Represents a list of objects that can be accessed by index. Provides methods to search, sort, and manipulate lists.