2018-04-28
How to Implement LINQ methods in JavaScript - Part 8
blogentry, programming, quicktip, series
blogentry, programming, quicktip, series
Photo by Pau Casals on Unsplash LINQ methods (All, Contains, SequenceEqual) in this article are somehow related in a way that they are predicates (returns true or false).
Here are the methods covered so far.
In this article, I will cover following methods.
[table id=9 /]
The sample collections used in this part are shown as below.
private static List<Order> Orders = new List<Order>{ | |
new Order(id: 1, quantity: 40, orderDate: new DateTime(2018, 1,1,1,1,1,1)), | |
new Order(id: 2, quantity: 20, orderDate: new DateTime(2018, 2,2,2,2,2,2)), | |
new Order(id: 3, quantity: 30, orderDate: new DateTime(2018, 3,3,3,3,3,3)), | |
new Order(id: 4, quantity: 10, orderDate: new DateTime(2018, 4,4,4,4,4,4)), | |
new Order(id: 5, quantity: 20, orderDate: new DateTime(2018, 5,5,5,5,5,5)), | |
}; | |
private static List<Order> DomesticOrders = new List<Order>{ | |
new Order(id: 1, quantity: 40, orderDate: new DateTime(2018, 1,1,1,1,1,1)), | |
new Order(id: 11, quantity: 20, orderDate: new DateTime(2018, 11,2,2,2,2,2)), | |
new Order(id: 111, quantity: 450, orderDate: new DateTime(2018, 11,1,2,2,2,2)), | |
new Order(id: 1111, quantity: 230, orderDate: new DateTime(2018, 11,11,2,2,2,2)), | |
}; |
const orders = [ | |
{ id: 1, quantity: 40, orderDate: new Date(2018, 1, 1, 1, 1, 1) }, | |
{ id: 2, quantity: 20, orderDate: new Date(2018, 2, 2, 2, 2, 2) }, | |
{ id: 3, quantity: 30, orderDate: new Date(2018, 3, 3, 3, 3, 3) }, | |
{ id: 4, quantity: 10, orderDate: new Date(2018, 4, 4, 4, 4, 4) }, | |
{ id: 5, quantity: 20, orderDate: new Date(2018, 5, 5, 5, 5, 5) } | |
]; | |
const domesticOrders = [ | |
{ id: 1, quantity: 40, orderDate: new Date(2018, 1, 1, 1, 1, 1) }, | |
{ id: 11, quantity: 20, orderDate: new Date(2018, 11, 2, 2, 2, 2) }, | |
{ id: 111, quantity: 450, orderDate: new Date(2018, 11, 1, 2, 2, 2) }, | |
{ id: 1111, quantity: 230, orderDate: new Date(2018, 11, 11, 2, 2, 2) } | |
]; |
JavaScript has a method called Array#every, which works the same way as All does.
private static void AllDemo(List<Order> orders) | |
{ | |
var areAllOrdersPlacedOn2018 = orders.All(order => order.OrderDate.Year == 2018); | |
WriteLine($"Are All Orders Placed On 2018?: {areAllOrdersPlacedOn2018}"); | |
} |
function allDemo(orders) { | |
const areAllOrdersPlacedOn2018 = orders.every( | |
order => order.orderDate.getFullYear() === 2018 | |
); | |
WriteLine(`Are All Orders Placed On 2018?: ${areAllOrdersPlacedOn2018}`); | |
} |
// C# | |
==================== All DEMO - Check If All Orders Match a Condition ==================== | |
Are All Orders Placed On 2018?: True | |
// JS | |
==================== All DEMO - Check If All Orders Match a Condition ==================== | |
Are All Orders Placed On 2018?: true |
every is more flexible as the callback is also passed an index.
There are so many ways implement contains but i used some as it short circuits (returns as soon as a condition is met).
private static void ContainsDemo(List<Order> shippedOrders, List<Order> domesticOrders) | |
{ | |
var firstDomesticOrder = domesticOrders.First(); | |
var containsDomesticOrder = shippedOrders.Contains(firstDomesticOrder, new OrderEqualityCompaprer()); | |
WriteLine($"Is the first domestic order shipped? {containsDomesticOrder}"); | |
} |
function containsDemo(shippedOrders, domesticOrders) { | |
const firstDomesticOrder = domesticOrders[0]; | |
const equalityComparer = order => order.id === firstDomesticOrder.id; | |
const containsDomesticOrder = shippedOrders.some(equalityComparer); | |
WriteLine(`Is the first domestic order shipped? ${containsDomesticOrder}`); | |
} |
// C# | |
==================== Contains DEMO - Do Shipped Orders Contain a Domestic Order? ==================== | |
Is the first domestic order shipped? True | |
// JS | |
==================== Contains DEMO - Do Shipped Orders Contain a Domestic Order? ==================== | |
Is the first domestic order shipped? true |
OrderEqualityComparer object instance simply checks if two objects are the same by an ID. JavaScript version simply passes a callback which does the same.
You can use every here to check if two sequences are the same as every passes an index of current element to the callback.
private static void SequenceEqualDemo(List<Order> shippedOrders, List<Order> domesticOrders) | |
{ | |
var sameOrderAreSame = shippedOrders.SequenceEqual(shippedOrders); | |
WriteLine($"Same Orders share the same sequence {sameOrderAreSame}"); | |
var areAllDomesticOrdersShipped = shippedOrders.SequenceEqual(domesticOrders); | |
WriteLine($"Are All Domestic Order Shipped? {areAllDomesticOrdersShipped}"); | |
} |
function sequenceEqualDemo(shippedOrders, domesticOrders) { | |
const sameOrderAreSame = shippedOrders.every( | |
(order, i) => order.id === shippedOrders[i].id | |
); | |
WriteLine(`Same Orders share the same sequence ${sameOrderAreSame}`); | |
const areAllDomesticOrdersShipped = shippedOrders.every( | |
(order, i) => order.id === domesticOrders[i].id | |
); | |
WriteLine(`Are All Domestic Order Shipped? ${areAllDomesticOrdersShipped}`); | |
} |
// C# | |
==================== SequenceEqual DEMO - Check If Two Sequences Are The Same ==================== | |
Same Orders share the same sequence True | |
Are All Domestic Order Shipped? False | |
// JS | |
==================== SequenceEqual DEMO - Check If Two Sequences Are The Same ==================== | |
Same Orders share the same sequence true | |
Are All Domestic Order Shipped? false |
I hope this article can serve as a cheat sheet (that's how I started this series).
Please let me know should you find any errors or improvements I can make to the codes. The full source code and instructions on how to run them are on GitHub.