Intro
One thing that a lot of .Net Developers love is System.Linq
. Linq is a powerful collection of extension methods when working with Enumerables.
This post will compare common .Net Enumerable methods with JavaScript Array methods.
.Any()
/ .some()
Check if at least one element in the sequence satisfies the condition.
Return type: Boolean
.
When used on an empty sequence the return value will be false
for any given condition.
Examples
let arr = [1, 4, 5, 6];
arr.some(c => c > 6); // false
arr.some(c => c > 5); // true
arr.some(); // Uncaught TypeError: undefined is not a function
arr.some(_ => _); // true
[].some(c => c > 5); // false
[].some(_ => _); // false
var list = new List<int>() { 1, 4, 5, 6 };
list.Any(c => c > 6); // false
list.Any(c => c > 5); // true
list.Any(); // true
new List<int>().Any(); // false
Extending
You might have noticed that we cannot use .some()
without passing the callback function. In this approach, we're adding the any()
function to the Array
type, so we can use it without a parameter similiar to .Nets .Any()
.
Array.prototype.any = function(callbackfn) {
if(typeof callbackfn !== 'undefined') {
return this.some(callbackfn);
} else {
return this.some(_ => _);
}
}
[].any(); // false
[1].any(); // true
[1].any(c => c === 1); // true
[1].any(c => c === 2); // false
.All()
/ .every()
Check if all elements in the sequence satisfy the condition.
Return type: Boolean
.
When used on an empty sequence the return value will be true
for any given condition.
Examples
let arr = [1, 4, 5, 6];
arr.every(c => c > 0); // true
arr.every(c => c > 5); // false
[].every(c => c > 5); // true
[].every(); // Uncaught TypeError: undefined is not a function
[].every(_ => _); // false
var list = new List<int>() { 1, 4, 5, 6 };
list.All(c => c > 0); // true
list.All(c => c > 5); // false
.Select()
/ .map()
Execute the given method on every element in the sequence and return the new sequence. Very useful to convert all elements within the sequence to another representation/type.
Examples
let arr = [1, 4, 5, 6];
arr.map(c => c * 2); // [2, 8, 10, 12]
arr.map(String); // ["1", "4", "5", "6"]
var list = new List<int>() { 1, 4, 5, 6 };
list.Select(c => c * 2); // [2, 8, 10, 12]
list.Select(c => c.ToString()); // ["1", "4", "5", "6"]
.Where()
/ .filter()
Filter the sequence with the given condition and return the filtered sequence. If no element is found an empty sequence is returned.
Examples
let arr = [1, 4, 5, 6];
arr.filter(c => c > 4); // [5, 6]
arr.filter(c => c > 7); // []
var list = new List<int>() { 1, 4, 5, 6 };
list.Where(c => c > 4); // [5, 6]
list.Where(c => c > 7); // []
.OrderBy()
/ .sort()
Orders the sequence in ascending order.
The .sort()
Method accepts a compare function which gives you a lot of flexibility. In this example, we'll keep things simple though and define our compare function similiar to how .OrderBy()
would sort. Check the mdn docs for a full explanation.
Examples
let arr = [
{
name: 'first',
order: 2,
},
{
name: 'second',
order: 1,
},
{
name: 'third',
order: 3
}
];
arr.sort((a, b) => a.order > b.order ? 1 : -1).map(c => c.name); // ["second", "first", "third"]
public class Item
{
public string Name { get; set; }
public int Order{ get; set; }
};
var list = new List<Item>()
{
new Item() { Name = "First", Order = 2},
new Item() { Name = "Second", Order = 1},
new Item() { Name = "Third", Order = 3},
}
list.OrderBy(c => c.Order).Select(c => c.Name); // ["Second", "First", "Third"]
.FirstOrDefault()
/ .find()
Find the first element in the sequence that satisfied the condition. If no element is found .find()
returns undefined
and .FirstOrDefault()
returns null
.
There are also other flavours in .Net such as .LastOrDefault()
, .First()
and .Last()
which don't have an equivalent in JavaScript.
Examples
let arr = [1, 4, 5, 6];
arr.find(c => c > 4); // 5
arr.find(c => c > 7); // undefined
arr.find(_ => _); // 1
var list = new List<int>() { 1, 4, 5, 6 };
list.FirstOrDefault(c => c > 4); // 5
list.FirstOrDefault(c => c > 7); // 0 (default(int) ≙ 0)
list.FirstOrDefault(); // 1
Quick compare
.Net | JavaScript | Usage |
---|---|---|
.Any() |
.some() |
Check if at least one element in the sequence contains satisifies the condition |
.All() |
.every() |
Check if all elements of the sequence satisfy the condition |
.Select() |
.map() |
Convert all elements of the sequence into another representation |
.Where() |
.filter() |
Filter the sequence based on the given condition |
.OrderBy() |
.sort() |
Sort the sequence |
.FirstOrDefault() |
.find() |
Get the first element that satisfies the condition |
References
In the next post, we'll look at how we can implement popular Linq-Methods that don't have an equivalent Array method in JavaScript (e.g. .Distinct()
).
Top comments (0)