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.
// 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");
console.log(youngsters);
// map is equivalent to Select
var names = people.map(function (item) {
return item.name;
});
console.log("Just the names of people");
console.log(names);
// every is equivalent to All
var allUnder40 = people.every(function (item) {
return item.age < 40;
});
console.log("Are all people under 40?"); // false
console.log(allUnder40);
// some is equivalent to Any
var anyUnder30 = people.some(function (item) {
return item.age < 30;
});
console.log("Are any people under 30?");
console.log(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");
console.log(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");
console.log(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");
console.log(namesOfPeopleOver30OrderedDesc);
@TioNoob

This comment has been minimized.

Copy link

@TioNoob TioNoob commented Apr 12, 2016

Thanks, it is very useful.

@gbreeze

This comment has been minimized.

Copy link

@gbreeze gbreeze commented Apr 19, 2016

Thanks!

@TheRoadyBuddha

This comment has been minimized.

Copy link

@TheRoadyBuddha TheRoadyBuddha commented Apr 26, 2016

This is a great overview. Thanks for doing this.

@jjhampton

This comment has been minimized.

Copy link

@jjhampton jjhampton commented May 17, 2016

Thanks - I'm more familiar w/ the Javascript method syntax, so this is good for me to translate the other way when I come across LINQ methods!

@jfbueno

This comment has been minimized.

Copy link

@jfbueno jfbueno commented Aug 3, 2016

Thanks, it's really very useful.

@tabareh

This comment has been minimized.

Copy link

@tabareh tabareh commented Oct 20, 2016

Awesome!

@MartinGian

This comment has been minimized.

Copy link

@MartinGian MartinGian commented Dec 12, 2016

Thanks for this!

@cameron-sjo

This comment has been minimized.

Copy link

@cameron-sjo cameron-sjo commented May 12, 2017

Thanks for this! Another to possible add is find() and it's equivalent to Linq's FirstOrDefault() method.

@tyler-kearney

This comment has been minimized.

Copy link

@tyler-kearney tyler-kearney commented May 31, 2017

I feel first or default is better represented as

var mikeOrDefault = people.filter(function (item) { return item.name === "Mike"; })[0] || null;

Index [0] on an empty array will return undefined, so || null allows you to default to null
Alternatively, like in linq.js, you can specify an actual default value such as { name: "Default", age: 0 }

@johnhilts

This comment has been minimized.

Copy link

@johnhilts johnhilts commented Sep 24, 2017

.reduce also has similarity with .SelectMany

.find is similar to .First

@ModernRonin

This comment has been minimized.

Copy link

@ModernRonin ModernRonin commented Nov 8, 2017

people.slice(n) in JS is equivalent to people.Skip(n) in C#

@ardalis

This comment has been minimized.

Copy link

@ardalis ardalis commented Jan 31, 2018

Created a JSFiddle and updated to use ES6 syntax here:
https://jsfiddle.net/ardalis/esy9mgjr/1/

@lioobayoyo

This comment has been minimized.

Copy link

@lioobayoyo lioobayoyo commented Mar 20, 2018

FirstOfDefault() without argument is equivalent to array[0], or array[0] || defaultValue . find cannot be used without argument.

@intolerance

This comment has been minimized.

Copy link

@intolerance intolerance commented Apr 16, 2018

Thank you!

@ChrisColeTech

This comment has been minimized.

Copy link

@ChrisColeTech ChrisColeTech commented Jun 8, 2018

Ehh...it's not perfect (filter kinda works sometimes...does not work very well with dates), but if you're stuck working in angular or javascript you can try to modify it and see if it meets your purpose.

@SebastianGaud

This comment has been minimized.

Copy link

@SebastianGaud SebastianGaud commented Jul 13, 2018

Top

@samisohail

This comment has been minimized.

Copy link

@samisohail samisohail commented Jul 17, 2018

Really useful and to the point !

@gruckion

This comment has been minimized.

Copy link

@gruckion gruckion commented Jul 31, 2018

Really appreciate it!

@akgunerhan

This comment has been minimized.

Copy link

@akgunerhan akgunerhan commented Oct 26, 2018

thanks a lot

@Jessicaward

This comment has been minimized.

Copy link

@Jessicaward Jessicaward commented Oct 30, 2018

Thanks 👍

@allanegidio

This comment has been minimized.

Copy link

@allanegidio allanegidio commented Feb 5, 2019

thanks

@gamblePants

This comment has been minimized.

Copy link

@gamblePants gamblePants commented Feb 8, 2019

I feel first or default is better represented as

var mikeOrDefault = people.filter(function (item) { return item.name === "Mike"; })[0] || null;

Index [0] on an empty array will return undefined, so || null allows you to default to null
Alternatively, like in linq.js, you can specify an actual default value such as { name: "Default", age: 0 }

thank you :) just what i was looking for

@townsean

This comment has been minimized.

Copy link

@townsean 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

This comment has been minimized.

Copy link

@AbhijithSugunan AbhijithSugunan commented Apr 8, 2019

Awesome work!!! Thanks

@ZombieProtectionAgency

This comment has been minimized.

Copy link

@ZombieProtectionAgency 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

This comment has been minimized.

Copy link

@rloveless rloveless commented Jul 1, 2019

My life is now complete!!

@alex-jitbit

This comment has been minimized.

Copy link

@alex-jitbit alex-jitbit commented Aug 24, 2019

thank you for this

@Visp1024

This comment has been minimized.

Copy link

@Visp1024 Visp1024 commented Dec 3, 2019

thx!

@Siderite

This comment has been minimized.

Copy link

@Siderite 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

This comment has been minimized.

Copy link

@leikoman leikoman commented Jun 25, 2020

Thanks!!!

@amul047

This comment has been minimized.

Copy link

@amul047 amul047 commented Jul 20, 2020

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

@DanDiplo

This comment has been minimized.

Copy link
Owner Author

@DanDiplo DanDiplo commented Jul 20, 2020

@amul047 Thanks, have done!

@filipewguedes

This comment has been minimized.

Copy link

@filipewguedes filipewguedes commented Aug 6, 2020

You deserve a medal

@DangerousDetlef

This comment has been minimized.

Copy link

@DangerousDetlef DangerousDetlef commented Aug 28, 2020

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

@axispod

This comment has been minimized.

Copy link

@axispod axispod commented Oct 20, 2020

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

@noseratio

This comment has been minimized.

Copy link

@noseratio 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.