TypeScript counterparts for C# LINQ.
Source: TypeScript vs. C#: LINQ
All of below are copied from https://decembersoft.com in case that blog post disappears in future, I save it here for time keeping. Let me know if its a problem.
TypeScript has no equivalent for the language-integrated-natural-query aspect of LINQ. (hey, isn’t that literally the whole acronym?)
True, you can’t write the following LINQ statement in TypeScript
var adultUserNames = from u in users where u.Age >= 18 select u.Name;
However, the IEnumerable<T>
extension methods, which are at the heart of LINQ, have equivalents in TypeScript (or can be emulated).
Aggregate
var leftToRight = users.Aggregate(initialValue, (a, u) => );
const leftToRight = users.reduce((a, u) => , initialValue);const rightToLeft = users.reduceRight((a, u) => , initialValue);
All
var allReady = users.All(u => u.IsReady);
const allReady = users.every(u => u.isReady);
Any
var isDirty = users.Any(u => u.IsDirty);
const isDirty = users.some(u => u.isDirty);
Append
var allUsers = users.Append(oneMoreUser);
const allUsers = [ ...users, oneMoreUser ];
Average
var avgAge = users.Average(u => u.Age);
if (users.length < 1) { throw new Error('source contains no elements');}const avgAge = users.reduce((a, u) => a + u.age, 0) / users.length;
Cast
var people = users.Cast<Person>();
const people = users as Person[];
Concat
var allUsers = users.Concat(moreUsers);
const allUsers = [ ...users, ...moreUsers ];
Contains
var hasAdmin = users.Contains(admin);
const hasAdmin = users.includes(admin);
Count
var n = users.Count();
const n = users.length;
DefaultIfEmpty
var nonEmptyUsers = Enumerable.DefaultIfEmpty(users);
const nonEmptyUsers = users.length ? users : [ null ];
Distinct
var uniqueNames = users.Select(u => u.Name).Distinct();
const uniqueNames = Object.keys( users.map(u => u.name).reduce( (un, u) => ({ ...un, n }), {} ));
ElementAt
var nth = users.ElementAt(n);
if (n < 0 || n > users.length) { throw new Error('Index was out of range');}const nth = users[n];
ElementAtOrDefault
var nth = users.ElementAtOrDefault(n);
const nth = users[n];
Empty
var noUsers = IEnumerable.Empty<User>();
const noUsers: User[] = [];const noUsers = [] as User[];
Except
var maleUsers = users.Except(femaleUsers);
const maleUsers = users.filter(u => !femaleUsers.includes(u) );
First
var first = users.First();
if (users.length < 1) { throw new Error('Sequence contains no elements');}const first = users[0];
FirstOrDefault
var first = users.FirstOrDefault();
const first = users[0];
List.ForEach
users.ToList().ForEach(u => );
users.forEach(u => );
GroupBy
var usersByCountry = users.GroupBy(u => u.Country);
const usersByCountry = users.reduce((ubc, u) => ({ ...ubc, [u.country]: [ ...(ubc[u.country] || []), u ],}), {});
Intersect
var targetUsers = usersWhoClicked.Intersect(usersBetween25And45);
const targetUsers = usersWhoClicked.filter(u => usersBetween25And45.includes(u) );
Last
var last = users.Last();
if (users.length < 1) { throw new Error('Sequence contains no elements');}const last = users[users.length - 1];
LastOrDefault
var last = users.LastOrDefault();
const last = users[users.length - 1];
Max
var oldestAge = users.Max(u => u.Age);
if (users.length < 1) { throw new Error('source contains no elements');}const oldestAge = users.reduce((oa, u) => Math.max(oa, u.age), 0);
Min
var youngestAge = users.Min(u => u.Age);
if (users.length < 1) { throw new Error('source contains no elements');}const youngestAge = users.reduce((ya, u) => Math.min(ya, u.age), Number.MAX_VALUE);
OfType
var bots = users.OfType<Bot>();
OrderBy / ThenBy
var sorted = users.OrderBy(u => u.Age).ThenBy(u => u.Name);
const sorted = users.sort((a, b) => { const ageDiff = b.age - a.age; if (ageDiff) return ageDiff; return a.name.localeCompare(b.name); });
Reverse
var backwards = users.Reverse();
const backwards = users.reverse();
Select
var names = users.Select(u => u.Name);
const names = users.map(u => u.name);
SelectMany
var phoneNumbers = users.SelectMany(u => u.PhoneNumbers);
const phoneNumbers = users.reduce((pn, u) => [ ...pn, ...u.phoneNumbers ], []);
Single
var user = users.Single();
if (users.length > 1) { throw new Error('The input sequence contains more than one element');}else if (!users.length) { throw new Error('The input sequence is empty');}const user = users[0];
SingleOrDefault
var user = users.Single();
const user = users[0];
Skip
var otherUsers = users.Skip(n);
const otherUsers = users.filter((u, i) => i >= n);
SkipWhile
var otherUsers = users.SkipWhile(predicate);
let i = 0;while (i < users.length && predicate(users[i++]));const otherUsers = users.slice(i - 1);
Sum
var totalYears = users.Sum(u => u.Age);
if (users.length < 1) { throw new Error('source contains no elements');}const totalYears = users.reduce((ty, u) => ty + u, 0);
Take
var otherUsers = users.Take(n);
const otherUsers = users.filter((u, i) => i < n);
TakeWhile
var otherUsers = users.TakeWhile(predicate);
let i = 0;while (i < users.length && predicate(users[i++]));const otherUsers = users.slice(0, i - 1);
Union
var allUsers = someUser.Union(otherUsers);
const allUsers = otherUsers.reduce((au, u) => au.includes(u) ? au : [ ...au, u ]}), someUsers));
Where
var adults = users.Where(u => u.Age >= 18);
const adults = users.filter(u => u.age >= 18);
Zip
var matches = buyers.Zip(sellers, (b, s) => new { Buyer = b, Seller = s });
const matches = [];for (let i = 0; i < buyers.length && i < sellers.length; i++) { matches.push({ buyer: buyers[i], seller: sellers[i], });}