2018-04-14

How to Implement LINQ methods in JavaScript - Part 6

blogentry, programming, quicktip, series

banner

Photo by Baher Khairy on Unsplash I will cover methods for getting subset of sequence.

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.

[table id=6]

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

C#

https://gist.github.com/dance2die/50ecc824ac0690b748578638506c37f0

JavaScript

https://gist.github.com/dance2die/37d6e62df1b39297660d25172618028b

🔴 Examples

🔸 First/FirstOrDefault

https://gist.github.com/dance2die/2b0e7371513d10b396e3fdc86f225c2c

https://gist.github.com/dance2die/6a79b6f46445dfc4189ef507da18cf90

Results

https://gist.github.com/dance2die/630c89d4a1ca751a73702dd23f83304e

I've just used "first" in JavaScript to implement "First/FirstOrDefault". JavaScript is dynamically typed so it wouldn't know what default value is unless you specify unlike in C#.

🔸 Last/LastOrDefault

https://gist.github.com/dance2die/bafb9c165141c783febb91a2472b91d7

https://gist.github.com/dance2die/0ff8b541482d346b866f943dbb5af5fd

Results

https://gist.github.com/dance2die/4dd6aad78993228f6ea885637678c52d

Same as "first", I used "last" in JavaScript to implement both "Last/LastOrDefault"

🔸 DefaultIfEmpty

https://gist.github.com/dance2die/7fe8e3e97d6c6c8d15f028774963386c

https://gist.github.com/dance2die/10e63dd4b2533668c9b65afd6e9c496c

Results

https://gist.github.com/dance2die/98d00030a6159282bbc580e03d16814d

In JavaScript, filter returns an array of size 0 if no record is returned. So I am checking for the size and return nullOrder if the length is 0.

🔸 Skip/SkipWhile

https://gist.github.com/dance2die/a3c27c88d4bc36e07496ab1c69701dfe

https://gist.github.com/dance2die/f7d63fb3456beff63103efa94b1671b8

Results

https://gist.github.com/dance2die/c1dcef1c836a1d7d601df711af8657e2

The callback in Array.prototype.filter also has an access to current index being process. So the implementation for skip simply filters out records below the given count while skipWhile filters out records that do not match the predicate.

🔸 Take/TakeWhile

https://gist.github.com/dance2die/6c2ce82f63d7772a675dc0a3e073f136

https://gist.github.com/dance2die/84d3bb00fafdb1ced427fc349c4135d7

Results

https://gist.github.com/dance2die/5a50966c2239ff9a9c92d16127bc930c

Take is the opposite of skip so the boolean conditions are reversed.

🔴 Closing Remark

JavaScript is dynamically typed so returning a "default" value is tricky as a variable type is defined upon initialization. I've skipped implementing "OrDefault" methods for that reason.

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.