How to Implement LINQ methods in JavaScript - Part 4

blogentry, programming, quicktip, series


Photo by Chris Lawton on Unsplash This is the 4th part of the series and I will cover common Set operations in this article.

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

Here are the methods covered. [table id=4 /] There is no one-to-one equivalent in JavaScript, so I added Lodash equivalents in the table this time. For production codes, use Lodash as their implementation is thoroughly battle-tested.

The sample collections used in this part are shown as below. (Orders is same as last 3 series and I added DomesticOrders and InternationalOrders for examples this time)





🔴 Examples

🔸 Union

Union combines two sequences (of type IEnumerable<T>) into one without duplicates.





I've extended the Array prototype to make the JavaScript version look similar to the LINQ version. 📝NOTE: "Union" in Lodash is named _.union.

🔸 Intersect

"Intersect" compares two sequences and return another sequence with a "common" value.

How do you check for the "commonness"? Intersect in LINQ provides an overload that lets you specify how to compare each value in two sequences by passing an object of type implementing IEqualityComparer<T> interface.





Our contrived order array contains objects with a property called id so I passed a callback (idSelector) to let Array.prototype.intersect to use it to make a comparison for each value in two sequences.

📝NOTE: While Intersect in LINQ is similar to _.intersectionWith in Lodash, JavaScript version is roughly equivalent to _.intersectionBy.

🔸 Except (Difference)

"Except" compares two sequences and return a new sequence with values that exists in the caller but not in the compared.





The implementation of Array.prototype.except is almost same as intersect.

The only difference between Array.prototype.intersect and Array.prototype.except is whether to include the record in the other set or not.

Take a close look at callback in filter method.


📝NOTE: While Except in LINQ is similar to _.differenceWith in Lodash, JavaScript version is roughly equivalent to _.differenceBy.

🔴 Closing Remark

"Union", "Intersect", and "Except" are the most common Set operations. I hope this article helped you understand how to implement Set operations in JavaScript.

JavaScript implementations in the examples above are not production ready as it's not optimized/tested. So as I mentioned before, use Lodash for Set operations in JavaScript.

Any feedback or error reports are always welcome.

The full source code and instructions on how to run them are on GitHub.