-
Star
(807)
You must be signed in to star a gist -
Fork
(157)
You must be signed in to fork a gist
-
-
Save DanDiplo/30528387da41332ff22b to your computer and use it in GitHub Desktop.
// 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); |
Thanks a ton 👍
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);
@SergeyDorofeev Yeah, I keep meaning to update it with arrow functions, but they weren't widely available when I first wrote this!
Nice
Thanks so much!!
This is beautiful! ❤️
Omo!!! This is quality.
great
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.
I agree. I use find
as an equivalent to FirstOrDefault.
const firstYoungster = peoples.find(p => p.age < 30);
@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.I agree. I use
find
as an equivalent to FirstOrDefault.const firstYoungster = peoples.find(p => p.age < 30);
Hi. Yes. I agree now. Back in 2017 find was still quite new. Especially for production code.
Edit: and I'll admit, I was unaware of find at the time
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.
const firstOrDefault =[].at(0) || defaultValue
const lastOrDefault =[].at(-1) || defaultValue
Thanks so much!! You're are a life saver!
flatMap
is like SelectMany
let items = [
{ text: "a", stuff: [ 1, 2, 3] },
{ text: "b", stuff: [ 4, 5 ] },
{ text: "c", stuff: [ 6, 7, 8] },
{ text: "d", stuff: [ 9, 0] }
];
flatStuff() {
return items.flatMap(m => m.stuff)
}
console.log(flatStuff())
returns
[1,2,3,4,5,6,7,8,9,0]
Thanks bro