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. (Looking for features such as Lazy-evaluation or observables, linq.js and RxJS offers these).
In the following I’ll list the most used operations and their JavaScript equivalents cheat sheet style. Notice that I’m using Lambda expressions in JavaScript to get the code a bit more concise, if they aren’t available just replace expressions like:
(data) => {}
with
function(data) {}
All operations are executed against this example:
var persons = [ { firstname: 'Peter', lastname: 'Jensen', type: 'Person', age: 30 }, { firstname: 'Anne', lastname: 'Jensen', type: 'Person', age: 50 }, { firstname: 'Kurt', lastname: 'Hansen', type: 'Person', age: 40 } ];
Notice that some of the operations modifies the source array, if you don’t want that just clone it with:
persons.slice(0);
Every operation is named after the method’s name in LINQ:
var result = persons.All(person => person.type == "Person");
var result = persons.filter(person => person.type == 'Person').length == persons.length;
var result = persons.Concat(persons);
var result = persons.concat(persons);
var result = persons.Count();
var result = persons.length;
var lastnames = persons.Select(person => person.lastname); var result = lastnames.Distinct();
var lastnames = persons.map(person => person.lastname); var result = lastnames.filter((value, index) => lastnames.indexOf(value) == index);
var result = Enumerable.Empty<dynamic>();
var result = [];
var result = persons.First();
var result = persons[0]; if (!result) throw new Error('Expected at least one element to take first')
var result = persons.FirstOrDefault();
var result = persons[0];
var fullnames = new List<string>(); persons.ForEach(person => fullnames.Add(person.firstname + " " + person.lastname));
var fullnames = []; persons.forEach(person => fullnames.push(person.firstname + ' ' + person.lastname))
var result = persons.GroupBy(person => person.lastname);
var result = persons.reduce((previous, person) => { (previous[person.lastname] = previous[person.lastname] || []).push(person); return previous; }, []);
var result = persons.IndexOf(persons[2]);
var result = persons.indexOf(persons[2]);
var result = persons.Last();
var result = persons[persons.length-1]; if (!result) throw new Error('Expected at least one element to take last')
var result = persons.LastOrDefault();
var result = persons[persons.length-1];
var result = persons.OrderBy(person => person.firstname);
persons.sort((person1, person2) => person1.firstname.localeCompare(person2.firstname));
var result = persons.OrderByDescending(person => person.firstname);
persons.sort((person1, person2) => person2.firstname.localeCompare(person1.firstname));
persons.Reverse();
var result = persons.reverse();
var result = persons.Select(person => new {fullname = person.firstname + " " + person.lastname});
var result = persons.map(person => ({ fullname: person.firstname + ' ' + person.lastname }) );
var result = persons.Single(person => person.firstname == "Peter");
var onePerson = persons.filter(person => person.firstname == "Peter"); if (onePerson.length != 1) throw new Error('Expected at excactly one element to take single') var result = onePerson[0];
var result = persons.Skip(2);
var result = persons.slice(2, persons.length);
var result = persons.Take(2);
var result = persons.slice(0, 2);
var result = persons.Where(person => person.lastname == "Jensen");
var result = persons.filter(person => person.lastname == 'Jensen');
So clearly you don’t need a library if you only need the basic operations. The code above with tests is available at GitHub.
This post is also available in Danish at QED.dk.
[…] This post is also available in English at Complexitymaze.com. […]
You should use [].every instead of the unnecessary [].filter combined with [].length when doing the LINQ equivalent of .All()
persons.every(person => person.type == ‘Person’)
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/every
@Kevin Reed, I did not know about every(). Thanks for the feedback
Did not know about Array.prototype.every as well. Cheers Kevin.
You can use JavaScriptLinq http://www.javascriptlinq.com/ It havs all linq functions
persons.Any(p=>p.Type == “person”)
persons.some(p=>p.type == “person”)
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/some
Here is one missing function
Select
C#
var result = persons.Select(person => new {fullname = person.firstname + ” ” + person.lastname});
JavaScript
var result = persons.Select(function(person) {return {fullname = person.firstname + ” ” + person.lastname});
Check out an implementation of LINQ in Javascript using modern ES6 language features: https://siderite.dev/blog/linq-in-javascript-linqer/