Here are the series covered so far.
Here are the methods covered.
And I will try to stick to using Vanilla JS as I have so far.
The sample collection used in this part is the same as all previous ones but I will list again.
OK, now let’s dive into some examples 👍.
Reverse is different from OrderByDescending that you don’t specify which element to reverse with. It simply reverses the current sequence in opposite order.
So to make the demo show how reverse works, I flipped first half and last half of the orders and reversed the list.
And now let’s move onto the next example, Zip, which is you might use seldom.
Note that Lodash has zipWith method which works the same as the LINQ version (Maybe I will write LINQ -> Lodash equivalent methods as a next series 😉).
We can simply retrieve the matching second collection’s item (
const text = orderNumbersInText[index]) with the callback’s index.
Refer to a simpler case on this StackOverflow answer.
Although Lodash has a similar methods, minBy and maxBy that works the same way as LINQ, I will show you a way to implement it using Math.min and Math.max. (I’ve combined two methods since both
max are called the same way)
You can use “Math.min/max” functions to emulate LINQ versions of Min/Max.
Notice that spread syntax (
...orders) is used within Math.min/max functions because those functions accept a list of values, not an array, So the array destructuring is required.
If you use “min/max” frequently or make it look just like the LINQ version, you can simply extend the Array prototype as shown below.
Now the it looks the same as the LINQ version.
🔴 Closing Remark
The full source code and instructions on how to run them are on GitHub.