Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
JavaScript equivalents of some common C# LINQ methods. To help me remember!
// JS array equivalents to C# LINQ methods - by Dan B.
// First: This version using older JavaScript notation for universal browser support (scroll down for ES6 version):
// Here's a simple array of "person" objects
var people = [
{ name: "John", age: 20 },
{ name: "Mary", age: 35 },
{ name: "Arthur", age: 78 },
{ name: "Mike", age: 27 },
{ name: "Judy", age: 42 },
{ name: "Tim", age: 8 }
];
// filter is equivalent to Where
var youngsters = people.filter(function (item) {
return item.age < 30;
});
console.log("People younger than 30:", youngsters);
// map is equivalent to Select
var names = people.map(function (item) {
return item.name;
});
console.log("Just the names of people:", names);
// every is equivalent to All
var allUnder40 = people.every(function (item) {
return item.age < 40;
});
console.log("Are all people under 40?", allUnder40); // false
// some is equivalent to Any
var anyUnder30 = people.some(function (item) {
return item.age < 30;
});
console.log("Are any people under 30?", anyUnder30); // true
// reduce is "kinda" equivalent to Aggregate (and also can be used to Sum)
var aggregate = people.reduce(function (item1, item2) {
return { name: '', age: item1.age + item2.age };
});
console.log("Aggregate age", aggregate.age); // { age: 210 }
// sort is "kinda" like OrderBy (but it sorts the array in place - eek!)
var orderedByName = people.sort(function (a, b) {
return a.name < b.name ? 1 : -1;
})
console.log("Ordered by name:", orderedByName);
// and, of course, you can chain function calls
var namesOfPeopleOver30OrderedDesc = people.filter(function (person) {
return person.age > 30;
}).
map(function (person) {
return person.name;
}).
sort(function (a, b) {
return a > b ? 1 : -1;
});
console.log("And now.. the names of all people over 30 ordered by name descending:", namesOfPeopleOver30OrderedDesc);
// Second: And now the more modern ES6 way of doing this using arrow functions (lambdas!)...
const peoples = [
{ name: "John", age: 20 },
{ name: "Mary", age: 35 },
{ name: "Arthur", age: 78 },
{ name: "Mike", age: 27 },
{ name: "Judy", age: 42 },
{ name: "Tim", age: 8 }
];
// filter is equivalent to Where
const youngPeople = peoples.filter(p => p.age < 30);
console.log("People younger than 30:", youngPeople);
// map is equivalent to Select
const justNames = peoples.map(p => p.name);
console.log("Just the names of people:", justNames);
// every is equivalent to All
const peopleUnder40 = peoples.every(p => p.age < 40);
console.log("Are all people under 40?", peopleUnder40); // false
// some is equivalent to Any
const areAnyUnder30 = peoples.some(p => p.age < 30);
console.log("Are any people under 30?", areAnyUnder30); // true
// reduce is "kinda" equivalent to Aggregate (and also can be used to Sum)
const aggregatedAge = peoples.reduce((p1, p2) => {
return { name: '', age: p1.age + p2.age }
});
console.log("Aggregate age:", aggregatedAge.age); // { age: 210 }
// sort is "kinda" like OrderBy (but it sorts the array in place - eek!)
const peopleOrderedByName = peoples.sort((p1, p2) => p1.name < p2.name ? 1 : -1);
console.log("Ordered by name:", peopleOrderedByName);
// and, of course, you can chain function calls
const peepsOver30OrderedDesc = peoples.filter(p => p.age > 30).map(p => p.name).sort((p1, p2) => p1 > p2 ? 1 : -1);
console.log("Chained", peepsOver30OrderedDesc);
@townsean
Copy link

townsean commented Mar 19, 2019

this is amazing! Thanks for compiling this. Coming from a C# background I'm always asking myself, 'what's the JavaScript equivalent to this?"

@AbhijithSugunan
Copy link

AbhijithSugunan commented Apr 8, 2019

Awesome work!!! Thanks

@ZombieProtectionAgency
Copy link

ZombieProtectionAgency commented Apr 18, 2019

@tyler-kearney

I dont think filter is a good way to represent FirstOrDefault. It functions the same but one of the primary benefits of FirstOrDefault is that it stops early. Filter has to parse the entire list before anything can be returned.

@rloveless
Copy link

rloveless commented Jul 1, 2019

My life is now complete!!

@alex-jitbit
Copy link

alex-jitbit commented Aug 24, 2019

thank you for this

@Visp1024
Copy link

Visp1024 commented Dec 3, 2019

thx!

@Siderite
Copy link

Siderite commented Jan 6, 2020

The Array functions in Javascript are similar in result, but not in implementation with C# LINQ. If you have an array with a million rows and you filter it, then map it, then get the first item, you get an array at each step: a million filterings and a thousand item array, a thousand mappings in another thousand item array. all for one item. I've researched a LINQ like approach in Javascript here: https://siderite.dev/blog/linq-in-javascript-linqer/

@leikoman
Copy link

leikoman commented Jun 25, 2020

Thanks!!!

@amul047
Copy link

amul047 commented Jul 20, 2020

Thanks, you may want to update the order by stuff - https://gist.github.com/amul047/5fa79670adcc8693269c066096019a0f/revisions

@DanDiplo
Copy link
Author

DanDiplo commented Jul 20, 2020

@amul047 Thanks, have done!

@filipewguedes
Copy link

filipewguedes commented Aug 6, 2020

You deserve a medal

@DangerousDetlef
Copy link

DangerousDetlef commented Aug 28, 2020

Nice overview, thank you very much. For someone coming from C# this is very helpful.

@axispod
Copy link

axispod commented Oct 20, 2020

But it has only one problem, a huge problem. LINQ is totally lazy, but JS function isn't.

@noseratio
Copy link

noseratio commented Oct 29, 2020

@axispod check out iterable-query - Query API for JavaScript Iterables and Async Iterables, I think it's very close to C# lazy LINQ.

@Workshop2
Copy link

Workshop2 commented Jan 22, 2021

Thanks so much

@aquino-a
Copy link

aquino-a commented Mar 10, 2021

nice

@uim-akhovyev
Copy link

uim-akhovyev commented Mar 15, 2021

Thank you!

@Kbllo
Copy link

Kbllo commented May 3, 2021

Thank you :D

@Serrin
Copy link

Serrin commented Jun 13, 2021

// sort is "kinda" like OrderBy (but it sorts the array in place - eek!)

var orderedByName = people.sort(function (a, b) {
	return  a.name > b.name ? 1 : -1;
})

console.log("Ordered by name"); 

console.log(orderedByName);

You can use this if you want to sort in a new array without change the original array (not in place):

var orderedByName = people.slice().sort(function (a, b) {
	return  a.name > b.name ? 1 : -1;
})

console.log("Ordered by name"); 
console.log(orderedByName);

console.log("Original array"); 
console.log(people);

@DanDiplo
Copy link
Author

DanDiplo commented Jun 15, 2021

Good shout @Serrin

@karmamaster
Copy link

karmamaster commented Jun 24, 2021

Thanks bro

@rskhan167
Copy link

rskhan167 commented Jul 30, 2021

Thanks a ton 👍

@SergeyDorofeev
Copy link

SergeyDorofeev commented Dec 3, 2021

It can also be written using arrow functions:

  const youngsters = people.filter(item => item.age < 30);
  const allUnder40 = people.every(item => item.age < 40); 
  const orderedByName = people.sort((a, b) => a.name > b.name ? 1 : -1);

@DanDiplo
Copy link
Author

DanDiplo commented Dec 3, 2021

@SergeyDorofeev Yeah, I keep meaning to update it with arrow functions, but they weren't widely available when I first wrote this!

@kigold
Copy link

kigold commented Mar 20, 2022

Nice

@euwiqo4487
Copy link

euwiqo4487 commented Apr 11, 2022

Thanks so much!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment