How to Implement LINQ methods in JavaScript – Part 8

featured
April 28, 2018
💫 Originally posted here. Broken? Let me know ~

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.

  1. Part 1 〰️ Select, Aggregate, Where, OrderBy (Ascending, Descending)
  2. Part 2 〰️ Any, Distinct, Concat, SelectMany
  3. Part 3 〰️ Reverse, Zip, Min/Max
  4. Part 4 〰️ Union, Intersect, Except
  5. Part 5 〰️ Sum, Average, Count
  6. Part 6 〰️ First, Last, DefaultIfEmpty, Skip, Take
  7. Part 7 〰️ Empty, Repeat, Range
  8. Part 8 〰️ All, Contains, SequenceEqual

🔴 Overview

In this article, I will cover following methods.

LINQ MethodsJavaScript MethodsGo to example
AlleveryAll
ContainssomeContains
SequenceEqualeverySequenceEqual

The sample collections used in this part are shown as below.

C#
JavaScript

🔴 Examples

🔸 All

JavaScript has a method called Array#every, which works the same way as All does.

Results

every is more flexible as the callback is also passed an index.

array#every syntax

🔸 Contains

There are so many ways implement contains but i used some as it short circuits (returns as soon as a condition is met).

Results

OrderEqualityComparer object instance simply checks if two objects are the same by an ID. JavaScript version simply passes a callback which does the same.

🔸 SequenceEqual

You can use every here to check if two sequences are the same as every passes an index of current element to the callback.

Results

🔴 Closing Remark

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.