Thursday, 4 August 2016

Collection iteration cheat sheet

Having not found a great cheat sheet between Linq and JavaScript Array methods I thought I would create one. The JS Array methods I have included here are supported from IE9 so I imagine most applications should be able to use them (and if not the Mozilla Developer Network provides some polyfills).

Be aware that chaining methods on the Array prototype will cause the collection to be iterated many times (unlike LINQ which will iterate once when the collection is enumerated). This is because each method returns a new array.

Underscore allows single iteration chaining by using the chain and value methods.

LINQ JS Array (ES6) Underscore (ES6) jQuery (ES6)
Any some some N/A
All every every N/A
Selectmap map map
Wherefilter filter grep
First/FirstOrDefault find find N/A

forEach each each
Max N/A max N/A
Min N/A min N/A
ToDictionary N/A groupBy N/A
Contains includes contains N/A
N/A findIndex indexOf inArray
Reverse reverse N/A N/A
String.Join ["One", "Two"].join(", ") N/A N/A

If I have missed anything please comment below and I will update the list.

No comments:

Post a Comment