{"id":1629,"date":"2014-04-03T09:19:38","date_gmt":"2014-04-03T07:19:38","guid":{"rendered":"http:\/\/complexitymaze.com\/?p=1629"},"modified":"2014-04-03T09:19:38","modified_gmt":"2014-04-03T07:19:38","slug":"cheat-sheet-from-basic-linq-to-javascript","status":"publish","type":"post","link":"https:\/\/complexitymaze.com\/?p=1629","title":{"rendered":"Cheat sheet: from basic LINQ to JavaScript"},"content":{"rendered":"<p>As a developer in the .NET world where <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/bb397926.aspx\" target=\"_new\" rel=\"noopener\">LINQ<\/a> is first class citizen, when going to JavaScript it seems that some methods are missing. There is even <a href=\"http:\/\/jslinq.codeplex.com\" target=\"_new\" rel=\"noopener\">a<\/a> <a href=\"http:\/\/jscriptlinq.codeplex.com\" target=\"_new\" rel=\"noopener\">few<\/a> libraries that tries to remedy this, but if you are just looking to get the job done the most often used methods are right at hand. (Looking for features such as Lazy-evaluation or observables, <a href=\"http:\/\/linqjs.codeplex.com\" target=\"_new\" rel=\"noopener\">linq.js<\/a> and <a href=\"https:\/\/github.com\/Reactive-Extensions\/RxJS\" target=\"_new\" rel=\"noopener\">RxJS<\/a> offers these).<\/p>\n<p>In the following I&#8217;ll list the most used operations and their JavaScript equivalents cheat sheet style. Notice that I&#8217;m using Lambda expressions in JavaScript to get the code a bit more concise, if they aren&#8217;t available just replace expressions like:<\/p>\n<pre class=\"brush: jscript; light: true; title: ; notranslate\" title=\"\">\n(data) =&gt; {}<\/pre>\n<p>with <\/p>\n<pre class=\"brush: jscript; light: true; title: ; notranslate\" title=\"\">\nfunction(data) {}<\/pre>\n<p>All operations are executed against this example:<\/p>\n<pre class=\"brush: jscript; light: true; title: ; notranslate\" title=\"\">\nvar persons = &#x5B;\n    { firstname: 'Peter', lastname: 'Jensen', type: 'Person', age: 30 },\n    { firstname: 'Anne', lastname: 'Jensen', type: 'Person', age: 50  },\n    { firstname: 'Kurt', lastname: 'Hansen', type: 'Person', age: 40  }\n];<\/pre>\n<p>Notice that some of the operations modifies the source array, if you don&#8217;t want that just clone it with:<\/p>\n<pre class=\"brush: jscript; light: true; title: ; notranslate\" title=\"\">\npersons.slice(0);<\/pre>\n<p>Every operation is named after the method&#8217;s name in LINQ:<\/p>\n<div style=\"border:1px solid #efefef;padding: 5px 10px 0px;background-color:#fefefe;font-size: 14px\">\n\t<b>All<\/b><\/p>\n<div style=\"font-size:10px;margin: -12px 0 -15px\">C#<\/div>\n<pre class=\"brush: csharp; light: true; title: ; notranslate\" title=\"\">\n\tvar result = persons.All(person =&gt; person.type == &quot;Person&quot;);<\/pre>\n<div style=\"font-size:10px;margin: -12px 0 -15px\">JavaScript<\/div>\n<pre class=\"brush: jscript; light: true; title: ; notranslate\" title=\"\">\n\tvar result = persons.filter(person =&gt; person.type == 'Person').length == persons.length;<\/pre>\n<\/div>\n<p><\/p>\n<div style=\"border:1px solid #efefef;padding: 5px 10px 0px;background-color:#fefefe;font-size: 14px\">\n\t<b>Concat<\/b><\/p>\n<div style=\"font-size:10px;margin: -12px 0 -15px\">C#<\/div>\n<pre class=\"brush: csharp; light: true; title: ; notranslate\" title=\"\">\n\tvar result = persons.Concat(persons);<\/pre>\n<div style=\"font-size:10px;margin: -12px 0 -15px\">JavaScript<\/div>\n<pre class=\"brush: jscript; light: true; title: ; notranslate\" title=\"\">\n\tvar result = persons.concat(persons);<\/pre>\n<\/div>\n<p><\/p>\n<div style=\"border:1px solid #efefef;padding: 5px 10px 0px;background-color:#fefefe;font-size: 14px\">\n\t<b>Count<\/b><\/p>\n<div style=\"font-size:10px;margin: -12px 0 -15px\">C#<\/div>\n<pre class=\"brush: csharp; light: true; title: ; notranslate\" title=\"\">\n\tvar result = persons.Count();<\/pre>\n<div style=\"font-size:10px;margin: -12px 0 -15px\">JavaScript<\/div>\n<pre class=\"brush: jscript; light: true; title: ; notranslate\" title=\"\">\n\tvar result = persons.length;<\/pre>\n<\/div>\n<p><\/p>\n<div style=\"border:1px solid #efefef;padding: 5px 10px 0px;background-color:#fefefe;font-size: 14px\">\n\t<b>Distinct<\/b><\/p>\n<div style=\"font-size:10px;margin: -12px 0 -15px\">C#<\/div>\n<pre class=\"brush: csharp; light: true; title: ; notranslate\" title=\"\">\n\tvar lastnames = persons.Select(person =&gt; person.lastname);\n    var result = lastnames.Distinct();\n\t<\/pre>\n<div style=\"font-size:10px;margin: -12px 0 -15px\">JavaScript<\/div>\n<pre class=\"brush: jscript; light: true; title: ; notranslate\" title=\"\">\n\tvar lastnames = persons.map(person =&gt; person.lastname);\n    var result = lastnames.filter((value, index) =&gt; lastnames.indexOf(value) == index);<\/pre>\n<\/div>\n<p><\/p>\n<div style=\"border:1px solid #efefef;padding: 5px 10px 0px;background-color:#fefefe;font-size: 14px\">\n\t<b>Empty<\/b><\/p>\n<div style=\"font-size:10px;margin: -12px 0 -15px\">C#<\/div>\n<pre class=\"brush: csharp; light: true; title: ; notranslate\" title=\"\">\n\tvar result = Enumerable.Empty&lt;dynamic&gt;();<\/pre>\n<div style=\"font-size:10px;margin: -12px 0 -15px\">JavaScript<\/div>\n<pre class=\"brush: jscript; light: true; title: ; notranslate\" title=\"\">\n\tvar result = &#x5B;];<\/pre>\n<\/div>\n<p><\/p>\n<div style=\"border:1px solid #efefef;padding: 5px 10px 0px;background-color:#fefefe;font-size: 14px\">\n\t<b>First<\/b><\/p>\n<div style=\"font-size:10px;margin: -12px 0 -15px\">C#<\/div>\n<pre class=\"brush: csharp; light: true; title: ; notranslate\" title=\"\">\n\tvar result = persons.First();<\/pre>\n<div style=\"font-size:10px;margin: -12px 0 -15px\">JavaScript<\/div>\n<pre class=\"brush: jscript; light: true; title: ; notranslate\" title=\"\">\n\tvar result = persons&#x5B;0];\n    if (!result) throw new Error('Expected at least one element to take first')<\/pre>\n<\/div>\n<p><\/p>\n<div style=\"border:1px solid #efefef;padding: 5px 10px 0px;background-color:#fefefe;font-size: 14px\">\n\t<b>FirstOrDefault<\/b><\/p>\n<div style=\"font-size:10px;margin: -12px 0 -15px\">C#<\/div>\n<pre class=\"brush: csharp; light: true; title: ; notranslate\" title=\"\">\n\tvar result = persons.FirstOrDefault();<\/pre>\n<div style=\"font-size:10px;margin: -12px 0 -15px\">JavaScript<\/div>\n<pre class=\"brush: jscript; light: true; title: ; notranslate\" title=\"\">\n\tvar result = persons&#x5B;0];<\/pre>\n<\/div>\n<p><\/p>\n<div style=\"border:1px solid #efefef;padding: 5px 10px 0px;background-color:#fefefe;font-size: 14px\">\n\t<b>ForEach<\/b><\/p>\n<div style=\"font-size:10px;margin: -12px 0 -15px\">C#<\/div>\n<pre class=\"brush: csharp; light: true; title: ; notranslate\" title=\"\">\n\tvar fullnames = new List&lt;string&gt;();\n    persons.ForEach(person =&gt; fullnames.Add(person.firstname + &quot; &quot; + person.lastname));<\/pre>\n<div style=\"font-size:10px;margin: -12px 0 -15px\">JavaScript<\/div>\n<pre class=\"brush: jscript; light: true; title: ; notranslate\" title=\"\">\nvar fullnames = &#x5B;];\npersons.forEach(person =&gt; fullnames.push(person.firstname + ' ' + person.lastname))<\/pre>\n<\/div>\n<p><\/p>\n<div style=\"border:1px solid #efefef;padding: 5px 10px 0px;background-color:#fefefe;font-size: 14px\">\n\t<b>GroupBy<\/b><\/p>\n<div style=\"font-size:10px;margin: -12px 0 -15px\">C#<\/div>\n<pre class=\"brush: csharp; light: true; title: ; notranslate\" title=\"\">\n\tvar result = persons.GroupBy(person =&gt; person.lastname);<\/pre>\n<div style=\"font-size:10px;margin: -12px 0 -15px\">JavaScript<\/div>\n<pre class=\"brush: jscript; light: true; title: ; notranslate\" title=\"\">\n\tvar result = persons.reduce((previous, person) =&gt; {\n\t\t(previous&#x5B;person.lastname] = previous&#x5B;person.lastname] || &#x5B;]).push(person);\n    \treturn previous;\n\t}, &#x5B;]);<\/pre>\n<\/div>\n<p><\/p>\n<div style=\"border:1px solid #efefef;padding: 5px 10px 0px;background-color:#fefefe;font-size: 14px\">\n\t<b>IndexOf<\/b><\/p>\n<div style=\"font-size:10px;margin: -12px 0 -15px\">C#<\/div>\n<pre class=\"brush: csharp; light: true; title: ; notranslate\" title=\"\">\n\tvar result = persons.IndexOf(persons&#x5B;2]);<\/pre>\n<div style=\"font-size:10px;margin: -12px 0 -15px\">JavaScript<\/div>\n<pre class=\"brush: jscript; light: true; title: ; notranslate\" title=\"\">\n\tvar result = persons.indexOf(persons&#x5B;2]);<\/pre>\n<\/div>\n<p><\/p>\n<div style=\"border:1px solid #efefef;padding: 5px 10px 0px;background-color:#fefefe;font-size: 14px\">\n\t<b>Last<\/b><\/p>\n<div style=\"font-size:10px;margin: -12px 0 -15px\">C#<\/div>\n<pre class=\"brush: csharp; light: true; title: ; notranslate\" title=\"\">\n\tvar result = persons.Last();<\/pre>\n<div style=\"font-size:10px;margin: -12px 0 -15px\">JavaScript<\/div>\n<pre class=\"brush: jscript; light: true; title: ; notranslate\" title=\"\">\n\tvar result = persons&#x5B;persons.length-1];\n    if (!result) throw new Error('Expected at least one element to take last')<\/pre>\n<\/div>\n<p><\/p>\n<div style=\"border:1px solid #efefef;padding: 5px 10px 0px;background-color:#fefefe;font-size: 14px\">\n\t<b>LastOrDefault<\/b><\/p>\n<div style=\"font-size:10px;margin: -12px 0 -15px\">C#<\/div>\n<pre class=\"brush: csharp; light: true; title: ; notranslate\" title=\"\">\n\tvar result = persons.LastOrDefault();<\/pre>\n<div style=\"font-size:10px;margin: -12px 0 -15px\">JavaScript<\/div>\n<pre class=\"brush: jscript; light: true; title: ; notranslate\" title=\"\">\n\tvar result = persons&#x5B;persons.length-1];<\/pre>\n<\/div>\n<p><\/p>\n<div style=\"border:1px solid #efefef;padding: 5px 10px 0px;background-color:#fefefe;font-size: 14px\">\n\t<b>OrderBy<\/b><\/p>\n<div style=\"font-size:10px;margin: -12px 0 -15px\">C#<\/div>\n<pre class=\"brush: csharp; light: true; title: ; notranslate\" title=\"\">\n\tvar result = persons.OrderBy(person =&gt; person.firstname);<\/pre>\n<div style=\"font-size:10px;margin: -12px 0 -15px\">JavaScript<\/div>\n<pre class=\"brush: jscript; light: true; title: ; notranslate\" title=\"\">\n\tpersons.sort((person1, person2) =&gt; person1.firstname.localeCompare(person2.firstname));<\/pre>\n<\/div>\n<p><\/p>\n<div style=\"border:1px solid #efefef;padding: 5px 10px 0px;background-color:#fefefe;font-size: 14px\">\n\t<b>OrderByDescending<\/b><\/p>\n<div style=\"font-size:10px;margin: -12px 0 -15px\">C#<\/div>\n<pre class=\"brush: csharp; light: true; title: ; notranslate\" title=\"\">\n\tvar result = persons.OrderByDescending(person =&gt; person.firstname);<\/pre>\n<div style=\"font-size:10px;margin: -12px 0 -15px\">JavaScript<\/div>\n<pre class=\"brush: jscript; light: true; title: ; notranslate\" title=\"\">\n\tpersons.sort((person1, person2) =&gt; person2.firstname.localeCompare(person1.firstname));<\/pre>\n<\/div>\n<p><\/p>\n<div style=\"border:1px solid #efefef;padding: 5px 10px 0px;background-color:#fefefe;font-size: 14px\">\n\t<b>Reverse<\/b><\/p>\n<div style=\"font-size:10px;margin: -12px 0 -15px\">C#<\/div>\n<pre class=\"brush: csharp; light: true; title: ; notranslate\" title=\"\">\n\tpersons.Reverse();<\/pre>\n<div style=\"font-size:10px;margin: -12px 0 -15px\">JavaScript<\/div>\n<pre class=\"brush: jscript; light: true; title: ; notranslate\" title=\"\">\n\tvar result = persons.reverse();<\/pre>\n<\/div>\n<p><\/p>\n<div style=\"border:1px solid #efefef;padding: 5px 10px 0px;background-color:#fefefe;font-size: 14px\">\n\t<b>Select<\/b><\/p>\n<div style=\"font-size:10px;margin: -12px 0 -15px\">C#<\/div>\n<pre class=\"brush: csharp; light: true; title: ; notranslate\" title=\"\">\n\tvar result = persons.Select(person =&gt; new {fullname = person.firstname + &quot; &quot; + person.lastname});<\/pre>\n<div style=\"font-size:10px;margin: -12px 0 -15px\">JavaScript<\/div>\n<pre class=\"brush: jscript; light: true; title: ; notranslate\" title=\"\">\n\tvar result = persons.map(person =&gt; ({ fullname: person.firstname + ' ' + person.lastname }) );<\/pre>\n<\/div>\n<p><\/p>\n<div style=\"border:1px solid #efefef;padding: 5px 10px 0px;background-color:#fefefe;font-size: 14px\">\n\t<b>Single<\/b><\/p>\n<div style=\"font-size:10px;margin: -12px 0 -15px\">C#<\/div>\n<pre class=\"brush: csharp; light: true; title: ; notranslate\" title=\"\">\n\tvar result = persons.Single(person =&gt; person.firstname == &quot;Peter&quot;);<\/pre>\n<div style=\"font-size:10px;margin: -12px 0 -15px\">JavaScript<\/div>\n<pre class=\"brush: jscript; light: true; title: ; notranslate\" title=\"\">\n\tvar onePerson = persons.filter(person =&gt; person.firstname == &quot;Peter&quot;); \n    if (onePerson.length != 1) throw new Error('Expected at excactly one element to take single')\n    var result = onePerson&#x5B;0];<\/pre>\n<\/div>\n<p><\/p>\n<div style=\"border:1px solid #efefef;padding: 5px 10px 0px;background-color:#fefefe;font-size: 14px\">\n\t<b>Skip<\/b><\/p>\n<div style=\"font-size:10px;margin: -12px 0 -15px\">C#<\/div>\n<pre class=\"brush: csharp; light: true; title: ; notranslate\" title=\"\">\n\tvar result = persons.Skip(2);<\/pre>\n<div style=\"font-size:10px;margin: -12px 0 -15px\">JavaScript<\/div>\n<pre class=\"brush: jscript; light: true; title: ; notranslate\" title=\"\">\n\tvar result = persons.slice(2, persons.length); <\/pre>\n<\/div>\n<p><\/p>\n<div style=\"border:1px solid #efefef;padding: 5px 10px 0px;background-color:#fefefe;font-size: 14px\">\n\t<b>Take<\/b><\/p>\n<div style=\"font-size:10px;margin: -12px 0 -15px\">C#<\/div>\n<pre class=\"brush: csharp; light: true; title: ; notranslate\" title=\"\">\n\tvar result = persons.Take(2);<\/pre>\n<div style=\"font-size:10px;margin: -12px 0 -15px\">JavaScript<\/div>\n<pre class=\"brush: jscript; light: true; title: ; notranslate\" title=\"\">\n\tvar result = persons.slice(0, 2);<\/pre>\n<\/div>\n<p><\/p>\n<div style=\"border:1px solid #efefef;padding: 5px 10px 0px;background-color:#fefefe;font-size: 14px\">\n\t<b>Where<\/b><\/p>\n<div style=\"font-size:10px;margin: -12px 0 -15px\">C#<\/div>\n<pre class=\"brush: csharp; light: true; title: ; notranslate\" title=\"\">\n\tvar result = persons.Where(person =&gt; person.lastname == &quot;Jensen&quot;);<\/pre>\n<div style=\"font-size:10px;margin: -12px 0 -15px\">JavaScript<\/div>\n<pre class=\"brush: jscript; light: true; title: ; notranslate\" title=\"\">\n\tvar result = persons.filter(person =&gt; person.lastname == 'Jensen');<\/pre>\n<\/div>\n<p><\/p>\n<p>So clearly you don&#8217;t need a library if you only need the basic operations. The code above with tests is available at <a href=\"https:\/\/github.com\/poulfoged\/Linq-equivalent-operations-in-JavaScript\" target=\"_new\" rel=\"noopener\">GitHub<\/a>.<\/p>\n<p><em>This post is also available in Danish at <a href=\"http:\/\/qed.dk\/poul-foged\/2014\/04\/03\/linq-kenderens-guide-til-javascript\" target=\"_new\" rel=\"noopener\">QED.dk<\/a>.<\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>As a developer in the .NET world where LINQ is first class citizen, when going to JavaScript it seems that some methods are missing. There is even a few libraries that tries to remedy this, but if you are just looking to get the job done the most often used methods are right at hand. [&hellip;]<\/p>\n","protected":false},"author":4,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,5,6],"tags":[],"class_list":["post-1629","post","type-post","status-publish","format-standard","hentry","category-c","category-geeky","category-javascript-2"],"_links":{"self":[{"href":"https:\/\/complexitymaze.com\/index.php?rest_route=\/wp\/v2\/posts\/1629","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/complexitymaze.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/complexitymaze.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/complexitymaze.com\/index.php?rest_route=\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/complexitymaze.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1629"}],"version-history":[{"count":0,"href":"https:\/\/complexitymaze.com\/index.php?rest_route=\/wp\/v2\/posts\/1629\/revisions"}],"wp:attachment":[{"href":"https:\/\/complexitymaze.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1629"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/complexitymaze.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1629"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/complexitymaze.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1629"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}