Skip to content

Instantly share code, notes, and snippets.

@truseed
Forked from aaronpowell/selectMany.js
Created April 4, 2017 15:28
Show Gist options
  • Save truseed/ce9454f14480aebe104727aee2c94285 to your computer and use it in GitHub Desktop.
Save truseed/ce9454f14480aebe104727aee2c94285 to your computer and use it in GitHub Desktop.
LINQ SelectMany in JavaScript
Array.prototype.selectMany = function (fn) {
return this.map(fn).reduce(function (x, y) { return x.concat(y); }, []);
};
// usage
console.log([[1,2,3], [4,5,6]].selectMany(function (x) { return x; })); //[1,2,3,4,5,6]
console.log([{ a: [1,2,3] }, { a: [4,5,6] }].selectMany(function (x) { return x.a; }));
console.log([{ title: 'Some Blog', tags: ['being awesome'] }, { title: 'Some Other Blog', tags: ['still awesome', 'javascript'] } ].selectMany(function (blog) { return blog.tags; }));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment