-
-
Save MaciejLisCK/b2d9fb5a0306147d57d7d0aa5a6c1135 to your computer and use it in GitHub Desktop.
JavaScript equivalents of some common C# LINQ methods. To help me remember!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } | |
]; | |
// Where | |
var youngsters = people.filter(function (item) { | |
return item.age < 30; | |
}); | |
// Select | |
var names = people.map(function (item) { | |
return item.name; | |
}); | |
// All | |
var allUnder40 = people.every(function (item) { | |
return item.age < 40; | |
}); | |
// Any | |
var anyUnder30 = people.some(function (item) { | |
return item.age < 30; | |
}); | |
// 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); // { 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 : 0; | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment